SQLObjectServer.java
/***************************************************************************
Copyright 2012 Emily Estes
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************/
package net.metanotion.sqlc;
import java.io.InputStream;
import java.util.concurrent.ConcurrentHashMap;
import java.util.HashSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.metanotion.io.File;
import net.metanotion.io.FileSystem;
import net.metanotion.util.Dispatcher;
import net.metanotion.scripting.ObjectServer;
import net.metanotion.scripting.StructManager;
import net.metanotion.sqlc.parser.ParseException;
public final class SQLObjectServer implements ObjectServer {
private static final Logger logger = LoggerFactory.getLogger(SQLObjectServer.class);
private static final Load loader = new Load();
private static final String defaultFileExt = "sql";
private static final StructManager defaultSM = new StructManager();
private final ConcurrentHashMap<String,SQLClass> klazzes = new ConcurrentHashMap<String,SQLClass>();
private final FileSystem<? extends File> fs;
private final StructManager sm;
private final String fileExt;
private final boolean enableCaching;
public SQLObjectServer(final FileSystem<? extends File> fs) { this(fs, defaultSM, defaultFileExt, true); }
public SQLObjectServer(final FileSystem<? extends File> fs, final StructManager sm) {
this(fs, sm, defaultFileExt, true);
}
public SQLObjectServer(final FileSystem<? extends File> fs, final StructManager sm, final String fileExt) {
this(fs, sm, fileExt, true);
}
public SQLObjectServer(final FileSystem<? extends File> fs,
final StructManager sm,
final String fileExt,
final boolean enableCaching) {
this.fs = fs;
this.sm = sm;
this.fileExt = fileExt;
this.enableCaching = enableCaching;
}
public static SQLClass load(final InputStream in, final StructManager sm) {
try {
return loader.load(in, sm, new HashSet<String>());
} catch (ParseException pe) { throw new RuntimeException(pe); }
}
// Object Server
@Override public Dispatcher dispatcher(final String name) { return this.get(name); }
@Override public SQLClass get(final String name) {
logger.debug("Get: " + name);
if(enableCaching) {
final SQLClass cls = klazzes.get(name);
if(cls!=null) { return cls; }
}
final String fileName = "/" + name.replace('.', '/') + "." + fileExt;
logger.debug("File: " + fileName);
final File<File> f = this.fs.get(fileName);
try (final InputStream in = f.openInput()) {
final SQLClass cls = load(in, sm);
if(enableCaching) { klazzes.put(name, cls); }
return cls;
} catch (final Exception e) {
logger.debug("Exception Loading SQL class", e);
throw new RuntimeException(e);
}
}
}