import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.simantics.db.services.adaption.reflection.ThisResource2;
import org.simantics.scl.reflection.OntologyVersions;
import org.simantics.utils.FileUtils;
+import org.simantics.utils.threads.ThreadUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
public class AdapterRegistry2 {
+ private static final Logger LOGGER = LoggerFactory.getLogger(AdapterRegistry2.class);
+
public static final String ADAPTERS_FILE = "adapters.xml";
public static final String ADAPTERS = "adapters";
public static final String CONSTRUCTOR = "constructor";
static private AdapterRegistry2 instance = new AdapterRegistry2();
- Collection<AdapterInstaller> installers = new ArrayList<AdapterInstaller>();
- Map<AdapterInstaller, String> installerSources = new HashMap<AdapterInstaller, String>();
+ ConcurrentHashMap<AdapterInstaller, String> installerSources = new ConcurrentHashMap<>();
Collection<Exception> exceptions = new ArrayList<Exception>();
public static AdapterRegistry2 getInstance() {
}
private void addInstaller(AdapterInstaller installer, String sourceDesc) {
- installers.add(installer);
installerSources.put(installer, sourceDesc);
}
- private void handleException(Exception e, String fileName) {
+ private static void handleException(Exception e, String fileName) {
System.err.println("At " + fileName);
e.printStackTrace();
}
s.syncRequest(new ReadRequest() {
@Override
public void run(ReadGraph g) {
- for(AdapterInstaller t : installers) {
+ for(AdapterInstaller t : installerSources.keySet()) {
try {
t.install(g, service);
} catch (Exception e) {
}
public void initialize(BundleContext context) {
-
+ LOGGER.info("Initializing");
try {
DocumentBuilderFactory factory =
}
// TODO Listen bundles (install/uninstall)
+ List<Future<?>> waitFor = new ArrayList<>();
if (exceptions.isEmpty())
for (final Bundle b : context.getBundles()) {
- URL file = b.getEntry(ADAPTERS_FILE);
- if (file != null) {
- String fileName = new Path(b.getLocation()).append(file.getPath()).toString();
- try {
- DocumentBuilder builder = factory.newDocumentBuilder();
- builder.setErrorHandler(new ErrorHandler() {
-
- @Override
- public void error(SAXParseException exception)
- throws SAXException {
- // TODO Put this error somewhere
- System.err.println("Parse error at "
- + b.getSymbolicName() + "/adapters.xml" +
- " line " + exception.getLineNumber() +
- " column " + exception.getColumnNumber() + ":");
- System.err.println(exception.getMessage());
- }
-
- @Override
- public void fatalError(SAXParseException exception)
- throws SAXException {
- error(exception);
- }
-
- @Override
- public void warning(SAXParseException exception)
- throws SAXException {
- error(exception);
- }
-
- });
-
- //System.out.println("bundle=" + b.getSymbolicName());
- String text = FileUtils.getContents(file);
- text = OntologyVersions.getInstance().currentVersion(text);
- StringReader reader = new StringReader( text );
- InputSource inputSource = new InputSource( reader );
- Document doc = builder.parse( inputSource );
- reader.close();
- handleAdaptersDocument(loader(b), doc, fileName);
- } catch (Exception e) {
- handleException(e, fileName);
-
+ Future<?> submit = ThreadUtils.getNonBlockingWorkExecutor().submit(() -> {
+ URL file = b.getEntry(ADAPTERS_FILE);
+ if (file != null) {
+ String fileName = new Path(b.getLocation()).append(file.getPath()).toString();
+ try {
+ DocumentBuilder builder = factory.newDocumentBuilder();
+ builder.setErrorHandler(new ErrorHandler() {
+
+ @Override
+ public void error(SAXParseException exception) throws SAXException {
+ // TODO Put this error somewhere
+ System.err.println("Parse error at " + b.getSymbolicName() + "/adapters.xml"
+ + " line " + exception.getLineNumber() + " column "
+ + exception.getColumnNumber() + ":");
+ System.err.println(exception.getMessage());
+ }
+
+ @Override
+ public void fatalError(SAXParseException exception) throws SAXException {
+ error(exception);
+ }
+
+ @Override
+ public void warning(SAXParseException exception) throws SAXException {
+ error(exception);
+ }
+
+ });
+
+ // System.out.println("bundle=" + b.getSymbolicName());
+ String text = FileUtils.getContents(file);
+ text = OntologyVersions.getInstance().currentVersion(text);
+ StringReader reader = new StringReader(text);
+ InputSource inputSource = new InputSource(reader);
+ Document doc = builder.parse(inputSource);
+ reader.close();
+ handleAdaptersDocument(loader(b), doc, fileName);
+ } catch (Exception e) {
+ handleException(e, fileName);
+
+ }
}
- }
+ });
+ waitFor.add(submit);
+ }
+ // Let's wait in here
+ waitFor.forEach(f -> {
+ try {
+ f.get();
+ } catch (InterruptedException | ExecutionException e) {
+ LOGGER.error("Could not wait adapters to load", e);
}
+ });
+ LOGGER.info("Adapters installed");
} catch (Exception e) {
handleException(e, "(no file name available)");
}