From cad493bc69ad825d30cbb55b0db315d8a94dfad9 Mon Sep 17 00:00:00 2001 From: Willem Cazander Date: Fri, 7 Mar 2014 15:26:54 +0100 Subject: [PATCH] Fixed loader bug and refactory module with local interface and removed loader object from language tree. --- .../java/org/x4o/xml/eld/EldModuleLoader.java | 3 +- .../org/x4o/xml/eld/EldModuleLoaderCore.java | 14 +- .../eld/lang/ElementModuleBindingHandler.java | 10 +- .../org/x4o/xml/io/sax/X4ODebugWriter.java | 18 +- .../xml/lang/AbstractX4OLanguageModule.java | 59 ++--- .../xml/lang/DefaultX4OLanguageLoader.java | 204 +++++++++++------- .../org/x4o/xml/lang/X4OLanguageModule.java | 71 +----- .../x4o/xml/lang/X4OLanguageModuleLoader.java | 6 +- .../lang/X4OLanguageModuleLoaderResult.java | 39 ++++ .../x4o/xml/lang/X4OLanguageModuleLocal.java | 79 +++++++ .../xml/lang/phase/X4OPhaseLanguageInit.java | 15 +- .../lang/DefaultX4OLanguageLoaderTest.java | 8 +- .../lang/meta/MetaLanguageSiblingLoader.java | 4 +- .../resources/META-INF/mtest/mtest-lang.eld | 4 +- 14 files changed, 315 insertions(+), 219 deletions(-) create mode 100644 x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageModuleLoaderResult.java create mode 100644 x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageModuleLocal.java diff --git a/x4o-driver/src/main/java/org/x4o/xml/eld/EldModuleLoader.java b/x4o-driver/src/main/java/org/x4o/xml/eld/EldModuleLoader.java index 6c7327f..3af7d29 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/eld/EldModuleLoader.java +++ b/x4o-driver/src/main/java/org/x4o/xml/eld/EldModuleLoader.java @@ -33,6 +33,7 @@ import org.x4o.xml.io.X4OConnectionException; import org.x4o.xml.io.X4OReader; import org.x4o.xml.lang.X4OLanguage; import org.x4o.xml.lang.X4OLanguageModule; +import org.x4o.xml.lang.X4OLanguageModuleLocal; import org.x4o.xml.lang.X4OLanguageSession; import org.x4o.xml.lang.X4OLanguageModuleLoader; import org.x4o.xml.lang.X4OLanguageModuleLoaderException; @@ -78,7 +79,7 @@ public class EldModuleLoader implements X4OLanguageModuleLoader { * @throws X4OLanguageModuleLoaderException When eld language could not be loaded. * @see org.x4o.xml.lang.X4OLanguageModuleLoader#loadLanguageModule(org.x4o.xml.lang.X4OLanguageLocal, org.x4o.xml.lang.X4OLanguageModule) */ - public void loadLanguageModule(X4OLanguageLocal language,X4OLanguageModule languageModule) throws X4OLanguageModuleLoaderException { + public void loadLanguageModule(X4OLanguageLocal language,X4OLanguageModuleLocal languageModule) throws X4OLanguageModuleLoaderException { logger.fine("Loading name eld file from resource: "+eldResource); try { X4ODriver driver = null; diff --git a/x4o-driver/src/main/java/org/x4o/xml/eld/EldModuleLoaderCore.java b/x4o-driver/src/main/java/org/x4o/xml/eld/EldModuleLoaderCore.java index f9595d8..b3eaf1d 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/eld/EldModuleLoaderCore.java +++ b/x4o-driver/src/main/java/org/x4o/xml/eld/EldModuleLoaderCore.java @@ -26,7 +26,6 @@ import java.util.logging.Logger; import org.x4o.xml.conv.ObjectConverter; import org.x4o.xml.conv.text.ClassConverter; - import org.x4o.xml.eld.lang.AttributeAliasElement; import org.x4o.xml.eld.lang.BeanElement; import org.x4o.xml.eld.lang.DescriptionElement; @@ -37,20 +36,18 @@ import org.x4o.xml.eld.lang.ElementInterfaceBindingHandler; import org.x4o.xml.eld.lang.ElementModuleBindingHandler; import org.x4o.xml.eld.lang.ElementNamespaceBindingHandler; import org.x4o.xml.eld.lang.ModuleElement; - import org.x4o.xml.element.ElementBindingHandler; import org.x4o.xml.element.ElementClass; import org.x4o.xml.element.ElementClassAttribute; import org.x4o.xml.element.ElementNamespace; import org.x4o.xml.element.ElementNamespaceInstanceProvider; import org.x4o.xml.element.ElementNamespaceInstanceProviderException; - import org.x4o.xml.lang.X4OLanguage; import org.x4o.xml.lang.X4OLanguageLocal; import org.x4o.xml.lang.X4OLanguageClassLoader; -import org.x4o.xml.lang.X4OLanguageModule; import org.x4o.xml.lang.X4OLanguageModuleLoader; import org.x4o.xml.lang.X4OLanguageModuleLoaderException; +import org.x4o.xml.lang.X4OLanguageModuleLocal; /** * EldModuleLoaderCore provides a few basic elements for the core eld x4o language. @@ -90,7 +87,7 @@ public class EldModuleLoaderCore implements X4OLanguageModuleLoader { * @param languageModule The module to load it in. * @see org.x4o.xml.lang.X4OLanguageModuleLoader#loadLanguageModule(org.x4o.xml.lang.X4OLanguageLocal, org.x4o.xml.lang.X4OLanguageModule) */ - public void loadLanguageModule(X4OLanguageLocal language,X4OLanguageModule languageModule) throws X4OLanguageModuleLoaderException { + public void loadLanguageModule(X4OLanguageLocal language,X4OLanguageModuleLocal languageModule) throws X4OLanguageModuleLoaderException { // Config module meta data configLanguageModule(languageModule); @@ -193,15 +190,14 @@ public class EldModuleLoaderCore implements X4OLanguageModuleLoader { namespace.addElementClass(ec); } - private void configLanguageModule(X4OLanguageModule languageModule) { + private void configLanguageModule(X4OLanguageModuleLocal languageModule) { languageModule.setId("cel-module"); languageModule.setProviderName("Core Element Languag Module"); languageModule.setProviderHost(PP_CEL_PROVIDER_HOST); languageModule.setDescription("Core Element Language Module Loader"); - languageModule.setSourceResource(this.getClass().getSimpleName()); //TODO: check if oke. } - private void startAndAddNamespace(X4OLanguageLocal language,X4OLanguageModule languageModule,ElementNamespace namespace) throws X4OLanguageModuleLoaderException { + private void startAndAddNamespace(X4OLanguageLocal language,X4OLanguageModuleLocal languageModule,ElementNamespace namespace) throws X4OLanguageModuleLoaderException { try { namespace.getElementNamespaceInstanceProvider().start(language, namespace); } catch (ElementNamespaceInstanceProviderException e) { @@ -287,7 +283,7 @@ public class EldModuleLoaderCore implements X4OLanguageModuleLoader { * @param id The handler id. * @param description The handler descripion. */ - private void addBindingHandler(X4OLanguageModule languageModule,ElementBindingHandler handler,String id,String description) { + private void addBindingHandler(X4OLanguageModuleLocal languageModule,ElementBindingHandler handler,String id,String description) { handler.setId(id); handler.setDescription(description); languageModule.addElementBindingHandler(handler); diff --git a/x4o-driver/src/main/java/org/x4o/xml/eld/lang/ElementModuleBindingHandler.java b/x4o-driver/src/main/java/org/x4o/xml/eld/lang/ElementModuleBindingHandler.java index 265c5c1..9f64fe6 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/eld/lang/ElementModuleBindingHandler.java +++ b/x4o-driver/src/main/java/org/x4o/xml/eld/lang/ElementModuleBindingHandler.java @@ -32,9 +32,9 @@ import org.x4o.xml.element.ElementInterface; import org.x4o.xml.element.ElementNamespace; import org.x4o.xml.element.ElementNamespaceInstanceProvider; import org.x4o.xml.element.ElementNamespaceInstanceProviderException; -import org.x4o.xml.lang.X4OLanguageModule; import org.x4o.xml.lang.X4OLanguage; import org.x4o.xml.lang.X4OLanguageClassLoader; +import org.x4o.xml.lang.X4OLanguageModuleLocal; /** * An ParentLanguageElementConfigurator. @@ -44,7 +44,7 @@ import org.x4o.xml.lang.X4OLanguageClassLoader; * @author Willem Cazander * @version 1.0 Jan 19, 2007 */ -public class ElementModuleBindingHandler extends AbstractElementBindingHandler { +public class ElementModuleBindingHandler extends AbstractElementBindingHandler { private final static Class[] CLASSES_CHILD = new Class[] { ElementInterface.class, @@ -58,7 +58,7 @@ public class ElementModuleBindingHandler extends AbstractElementBindingHandler< * @see org.x4o.xml.element.ElementBindingHandler#getBindParentClass() */ public Class getBindParentClass() { - return X4OLanguageModule.class; + return X4OLanguageModuleLocal.class; } /** @@ -71,7 +71,7 @@ public class ElementModuleBindingHandler extends AbstractElementBindingHandler< /** * @see org.x4o.xml.element.AbstractElementBindingHandler#bindChild(org.x4o.xml.element.Element, java.lang.Object, java.lang.Object) */ - public void bindChild(Element childElement,X4OLanguageModule languageModule, Object childObject) throws ElementBindingHandlerException { + public void bindChild(Element childElement,X4OLanguageModuleLocal languageModule, Object childObject) throws ElementBindingHandlerException { X4OLanguage x4oParsingContext = EldModuleLoader.getLanguage(childElement.getLanguageSession()); if (x4oParsingContext==null) { @@ -159,7 +159,7 @@ public class ElementModuleBindingHandler extends AbstractElementBindingHandler< /** * @see org.x4o.xml.element.AbstractElementBindingHandler#createChilderen(org.x4o.xml.element.Element, java.lang.Object) */ - public void createChilderen(Element parentElement,X4OLanguageModule parent) throws ElementBindingHandlerException { + public void createChilderen(Element parentElement,X4OLanguageModuleLocal parent) throws ElementBindingHandlerException { for (ElementInterface child:parent.getElementInterfaces()) { createChild(parentElement, child); } diff --git a/x4o-driver/src/main/java/org/x4o/xml/io/sax/X4ODebugWriter.java b/x4o-driver/src/main/java/org/x4o/xml/io/sax/X4ODebugWriter.java index 048ff9a..64773be 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/io/sax/X4ODebugWriter.java +++ b/x4o-driver/src/main/java/org/x4o/xml/io/sax/X4ODebugWriter.java @@ -42,6 +42,7 @@ import org.x4o.xml.element.ElementNamespace; import org.x4o.xml.element.ElementNamespaceInstanceProvider; import org.x4o.xml.io.sax.ext.ContentWriter; import org.x4o.xml.lang.X4OLanguageModule; +import org.x4o.xml.lang.X4OLanguageModuleLoaderResult; import org.x4o.xml.lang.X4OLanguageSession; import org.x4o.xml.lang.X4OLanguageConfiguration; import org.x4o.xml.lang.phase.X4OPhase; @@ -223,13 +224,20 @@ public class X4ODebugWriter { atts.addAttribute ("", "id", "", "", module.getId()); atts.addAttribute ("", "providerName", "", "", module.getProviderName()); atts.addAttribute ("", "providerHost", "", "", module.getProviderHost()); - if (module.getLanguageModuleLoader()==null) { - atts.addAttribute ("", "elementLanguageModuleLoaderClassName", "", "", "null"); - } else { - atts.addAttribute ("", "elementLanguageModuleLoaderClassName", "", "", module.getLanguageModuleLoader().getClass().getName()); - } contentWriter.startElement (DEBUG_URI, "ElementLanguageModule", "", atts); + for (X4OLanguageModuleLoaderResult result:X4OLanguageModuleLoaderResult.values()) { + String value = module.getLoaderResult(result); + if (value==null) { + continue; + } + atts = new AttributesImpl(); + atts.addAttribute ("", "resultKey", "", "", result.name()); + atts.addAttribute ("", "resultValue", "", "", value); + contentWriter.startElement (DEBUG_URI, "ElementLanguageModuleResult", "", atts); + contentWriter.endElement(DEBUG_URI, "ElementLanguageModuleResult", ""); + } + debugElementConfiguratorGlobal(module.getElementConfiguratorGlobals()); debugElementBindingHandler(module.getElementBindingHandlers()); diff --git a/x4o-driver/src/main/java/org/x4o/xml/lang/AbstractX4OLanguageModule.java b/x4o-driver/src/main/java/org/x4o/xml/lang/AbstractX4OLanguageModule.java index 12d66d1..003c348 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/lang/AbstractX4OLanguageModule.java +++ b/x4o-driver/src/main/java/org/x4o/xml/lang/AbstractX4OLanguageModule.java @@ -40,18 +40,17 @@ import org.x4o.xml.element.ElementNamespace; * @author Willem Cazander * @version 1.0 Aug 2, 2012 */ -public abstract class AbstractX4OLanguageModule extends AbstractElementMetaBase implements X4OLanguageModule { +public abstract class AbstractX4OLanguageModule extends AbstractElementMetaBase implements X4OLanguageModuleLocal { private Logger logger = null; private String providerName=null; private String providerHost=null; - private String sourceResource = null; private List elementBindingHandlers = null; private List elementConfiguratorGlobals = null; private List elementInterfaces = null; private Map elementNamespaces = null; - private X4OLanguageModuleLoader elementLanguageModuleLoader = null; + private Map loaderResults = null; /** * Creates a new empty ElementLanguage. @@ -63,6 +62,7 @@ public abstract class AbstractX4OLanguageModule extends AbstractElementMetaBase elementConfiguratorGlobals = new ArrayList(4); elementInterfaces = new ArrayList(20); elementNamespaces = new HashMap(10); + loaderResults = new HashMap(10); } /** @@ -113,14 +113,14 @@ public abstract class AbstractX4OLanguageModule extends AbstractElementMetaBase logger.finer("Adding ElementBindingHandler: "+elementBindingHandler); elementBindingHandlers.add(elementBindingHandler); } - + /** * @see org.x4o.xml.lang.X4OLanguageModule#getElementBindingHandlers() */ public List getElementBindingHandlers() { return elementBindingHandlers; } - + /** * @see org.x4o.xml.lang.X4OLanguageModule#addElementConfiguratorGlobal(ElementConfiguratorGlobal) */ @@ -141,7 +141,7 @@ public abstract class AbstractX4OLanguageModule extends AbstractElementMetaBase public List getElementConfiguratorGlobals() { return elementConfiguratorGlobals; } - + /** * @see org.x4o.xml.lang.X4OLanguageModule#addElementInterface(org.x4o.xml.element.ElementInterface) */ @@ -157,14 +157,14 @@ public abstract class AbstractX4OLanguageModule extends AbstractElementMetaBase } elementInterfaces.add(elementInterface); } - + /** * @see org.x4o.xml.lang.X4OLanguageModule#getElementInterfaces() */ public List getElementInterfaces() { return elementInterfaces; } - + /** * @see org.x4o.xml.lang.X4OLanguageModule#addElementNamespace(org.x4o.xml.element.ElementNamespace) */ @@ -178,57 +178,32 @@ public abstract class AbstractX4OLanguageModule extends AbstractElementMetaBase logger.fine("Adding namespaceUri: "+elementNamespace.getUri()); elementNamespaces.put(elementNamespace.getUri(), elementNamespace); } - + /** * @see org.x4o.xml.lang.X4OLanguageModule#getElementNamespace(java.lang.String) */ public ElementNamespace getElementNamespace(String namespaceUri) { return elementNamespaces.get(namespaceUri); } - + /** * @see org.x4o.xml.lang.X4OLanguageModule#getElementNamespaces() */ public List getElementNamespaces() { return new ArrayList(elementNamespaces.values()); } - + /** - * @return the elementLanguageModuleLoader + * @see org.x4o.xml.lang.X4OLanguageModule#getLoaderResult(org.x4o.xml.lang.X4OLanguageModuleLoaderResult) */ - public X4OLanguageModuleLoader getLanguageModuleLoader() { - return elementLanguageModuleLoader; - } - - /** - * @param elementLanguageModuleLoader the elementLanguageModuleLoader to set - */ - public void setLanguageModuleLoader(X4OLanguageModuleLoader elementLanguageModuleLoader) { - this.elementLanguageModuleLoader = elementLanguageModuleLoader; - } - - /** - * @return the sourceResource - */ - public String getSourceResource() { - return sourceResource; - } - - /** - * @param sourceResource the sourceResource to set - */ - public void setSourceResource(String sourceResource) { - this.sourceResource = sourceResource; + public String getLoaderResult(X4OLanguageModuleLoaderResult key) { + return loaderResults.get(key); } /** - * Reloads the module, experiment !! + * @see org.x4o.xml.lang.X4OLanguageModuleLocal#putLoaderResult(org.x4o.xml.lang.X4OLanguageModuleLoaderResult, java.lang.String) */ - public void reloadModule(X4OLanguageLocal elementLanguage,X4OLanguageModule elementLanguageModule) throws X4OLanguageModuleLoaderException { - elementBindingHandlers.clear(); - elementInterfaces.clear(); - elementNamespaces.clear(); - - getLanguageModuleLoader().loadLanguageModule(elementLanguage, elementLanguageModule); + public void putLoaderResult(X4OLanguageModuleLoaderResult key, String value) { + loaderResults.put(key, value); } } diff --git a/x4o-driver/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageLoader.java b/x4o-driver/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageLoader.java index ebc8e80..e0a0dcd 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageLoader.java +++ b/x4o-driver/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageLoader.java @@ -26,14 +26,14 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.ArrayList; +import java.util.Date; import java.util.Enumeration; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.logging.Logger; import org.x4o.xml.eld.EldDriver; import org.x4o.xml.eld.EldModuleLoader; +import org.x4o.xml.lang.phase.X4OPhaseLanguageInit.X4OPhaseInitLanguageSiblings; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -50,14 +50,14 @@ import org.xml.sax.helpers.XMLReaderFactory; public class DefaultX4OLanguageLoader implements X4OLanguageLoader { private Logger logger = null; - protected List>> modulesAll = null; + protected List modulesAll = null; /** * Creates the DefaultX4OLanguageLoader. */ public DefaultX4OLanguageLoader() { logger = Logger.getLogger(DefaultX4OLanguageLoader.class.getName()); - modulesAll = new ArrayList>>(20); + modulesAll = new ArrayList(20); } /** @@ -82,77 +82,105 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) { * @see org.x4o.xml.lang.X4OLanguageLoader#loadLanguage(org.x4o.xml.lang.X4OLanguageLocal, java.lang.String, java.lang.String) */ public void loadLanguage(X4OLanguageLocal languageLocal, String language,String languageVersion) throws X4OLanguageLoaderException { + logger.finer("Loading all modules for language: "+language); try { - logger.finer("Loading all modules for language: "+language); loadLanguageModules(languageLocal,language); + } catch (IOException e) { + throw new X4OLanguageLoaderException(e); + } catch (SAXException e) { + throw new X4OLanguageLoaderException(e); + } + + X4OLanguageVersionFilter lvf; + try { + lvf = (X4OLanguageVersionFilter)X4OLanguageClassLoader.newInstance(languageLocal.getLanguageConfiguration().getDefaultLanguageVersionFilter()); + } catch (InstantiationException e) { + throw new X4OLanguageLoaderException(e); + } catch (IllegalAccessException e) { + throw new X4OLanguageLoaderException(e); + } + int loaded = 0; + for (VersionedResources versionedResources:modulesAll) { + List versions = new ArrayList(); + versions.add(versionedResources.version); // FIXME + String modulesVersion = lvf.filterVersion(languageVersion, versions); + if (modulesVersion==null) { + continue; + } - X4OLanguageVersionFilter lvf = (X4OLanguageVersionFilter)X4OLanguageClassLoader.newInstance(languageLocal.getLanguageConfiguration().getDefaultLanguageVersionFilter()); - - for (Map> map:modulesAll) { - List versions = new ArrayList(map.keySet()); - String modulesVersion = lvf.filterVersion(languageVersion, versions); - if (modulesVersion==null) { - throw new X4OLanguageLoaderException("No modules config for version: "+languageVersion); + X4OLanguageModuleLoader loader; + for (String value:versionedResources.eldResources) { + String languagePrefix = languageLocal.getLanguageConfiguration().getLanguageResourcePathPrefix(); + String resource = languagePrefix+"/"+language+"/"+value; + if (language.equals(EldDriver.LANGUAGE_NAME)) { + loader = new EldModuleLoader(resource,true); // load cel + } else { + loader = new EldModuleLoader(resource,false); // load eld } - Map modules = map.get(modulesVersion); - logger.finer("Filtered modules to version: "+modulesVersion); - if (modules==null) { - throw new X4OLanguageLoaderException("No modules defined for version: "+modulesVersion); + loadModule(languageLocal,loader,value,versionedResources); + } + for (String value:versionedResources.moduleLoaders) { + try { + loader = (X4OLanguageModuleLoader)X4OLanguageClassLoader.loadClass(value).newInstance(); + } catch (Exception ee) { + throw new X4OLanguageLoaderException("Could not load class: "+value+" error: "+ee.getMessage(),ee); } - - for (String key:modules.keySet()) { - String value = modules.get(key); - X4OLanguageModule module = (X4OLanguageModule)X4OLanguageClassLoader.newInstance(languageLocal.getLanguageConfiguration().getDefaultElementLanguageModule()); - module.setSourceResource(value); + loadModule(languageLocal,loader,value,versionedResources); + } + for (String value:versionedResources.siblingLoaders) { + try { + loader = (X4OLanguageModuleLoader)X4OLanguageClassLoader.loadClass(value).newInstance(); + } catch (Exception ee) { + throw new X4OLanguageLoaderException("Could not load class: "+value+" error: "+ee.getMessage(),ee); + } + loadModule(languageLocal,loader,value,versionedResources); + if (loader instanceof X4OLanguageModuleLoaderSibling) { - logMessage(languageLocal,"Parsing language config key: "+key+" value: "+value); - - if ("module-loader".equals(key)) { - try { - module.setLanguageModuleLoader( (X4OLanguageModuleLoader)X4OLanguageClassLoader.loadClass(value).newInstance() ); - } catch (Exception ee) { - throw new SAXException("Could not load: "+value+" error: "+ee.getMessage(),ee); - } - - } else if ("eld-resource".equals(key)) { - String languagePrefix = languageLocal.getLanguageConfiguration().getLanguageResourcePathPrefix(); - String resource = languagePrefix+"/"+language+"/"+value; - if (language.equals(EldDriver.LANGUAGE_NAME)) { - module.setLanguageModuleLoader(new EldModuleLoader(resource,true)); // load cel - } else { - module.setLanguageModuleLoader(new EldModuleLoader(resource,false)); // load eld - } - module.setSourceResource(resource); - } else if ("elb-resource".equals(key)) { - - // todo - logger.finer("elb-resources are not done yet."); - - } else if ("sibling-loader".equals(key)) { - try { - module.setLanguageModuleLoader( (X4OLanguageModuleLoaderSibling)X4OLanguageClassLoader.loadClass(value).newInstance() ); - } catch (Exception ee) { - throw new SAXException("Could not load: "+value+" error: "+ee.getMessage(),ee); - } - } - - if (module.getLanguageModuleLoader()==null) { - logger.warning("module with null loader: "+module+" tag: "+key+" chars: "+value); - continue; - } - - // mmm start in order ? - logMessage(languageLocal,"Starting modules: "+module+" for language: "+language); - module.getLanguageModuleLoader().loadLanguageModule(languageLocal, module); - - languageLocal.addLanguageModule(module); + X4OPhaseInitLanguageSiblings sibPhase = (X4OPhaseInitLanguageSiblings)languageLocal.getPhaseManager().getPhase("INIT_LANG_SIB"); + sibPhase.addLanguageModuleLoaderSibling((X4OLanguageModuleLoaderSibling)loader); } } - } catch (Exception e1) { - throw new X4OLanguageLoaderException(e1.getMessage()+" for language: "+language,e1); + for (String value:versionedResources.elbResources) { + // TODO: add elb support + logger.finer("elb-resources are not done yet; "+value); + } + loaded++; + } + if (loaded==0) { + throw new X4OLanguageLoaderException("No modules defined for version: "+languageVersion); } } + private void loadModule(X4OLanguageLocal languageLocal,X4OLanguageModuleLoader loader,String resource,VersionedResources versionedResources) throws X4OLanguageLoaderException { + X4OLanguageModuleLocal module; + try { + module = (X4OLanguageModuleLocal)X4OLanguageClassLoader.newInstance(languageLocal.getLanguageConfiguration().getDefaultElementLanguageModule()); + } catch (InstantiationException e) { + throw new X4OLanguageLoaderException(e); + } catch (IllegalAccessException e) { + throw new X4OLanguageLoaderException(e); + } + logMessage(languageLocal,"Created module: "+module); + long startTime = System.currentTimeMillis(); + try { + logMessage(languageLocal,"Starting modules: "+module+" for language: "+languageLocal.getLanguageName()); + loader.loadLanguageModule(languageLocal, module); + } catch (X4OLanguageModuleLoaderException e) { + throw new X4OLanguageLoaderException(e); // FIXME info + } + long totalTime = System.currentTimeMillis() - startTime; + module.putLoaderResult(X4OLanguageModuleLoaderResult.LOAD_TIME, ""+totalTime); + module.putLoaderResult(X4OLanguageModuleLoaderResult.LOAD_VERSION, versionedResources.version); + module.putLoaderResult(X4OLanguageModuleLoaderResult.LOAD_CLASS, loader.getClass().getName()); + module.putLoaderResult(X4OLanguageModuleLoaderResult.LOAD_DATE, ""+new Date()); + if (resource!=null) { + module.putLoaderResult(X4OLanguageModuleLoaderResult.LOAD_MODULE_RESOURCE, resource); + } + module.putLoaderResult(X4OLanguageModuleLoaderResult.LOAD_FROM_RESOURCE, versionedResources.loadedFrom); + + languageLocal.addLanguageModule(module); + } + /** * Loads all modules of an language. * @param languageLocal The ElementLanguage to load for. @@ -172,14 +200,14 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) { while(e.hasMoreElements()) { URL u = e.nextElement(); logMessage(languageLocal,"Loading relative modules: "+u+" for: "+language); - loadModulesXml(u.openStream()); + loadModulesXml(u.openStream(),u.toString()); } e = Thread.currentThread().getContextClassLoader().getResources("/"+buf.toString()); while(e.hasMoreElements()) { URL u = e.nextElement(); logMessage(languageLocal,"Loading root modules: "+u+" for: "+language); - loadModulesXml(u.openStream()); + loadModulesXml(u.openStream(),u.toString()); } } @@ -189,11 +217,11 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) { * @throws IOException * @throws SAXException */ - private void loadModulesXml(InputStream in) throws IOException, SAXException { + protected void loadModulesXml(InputStream in,String loadedFrom) throws IOException, SAXException { if (in==null) { throw new NullPointerException("Can't parse null input stream"); } - ModulesTagHandler xth = new ModulesTagHandler(); + ModulesTagHandler xth = new ModulesTagHandler(loadedFrom); XMLReader saxParser = XMLReaderFactory.createXMLReader(); saxParser.setContentHandler(xth); saxParser.setProperty("http://xml.org/sax/properties/lexical-handler", xth); @@ -207,17 +235,24 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) { private class ModulesTagHandler extends DefaultHandler2 { private StringBuffer buf = new StringBuffer(); - private String version = null; + private String loadedFrom = null; + private VersionedResources versionedResources = null; + + public ModulesTagHandler(String loadedFrom) { + this.loadedFrom=loadedFrom; + } @Override public void startDocument() throws SAXException { - modulesAll.add(new HashMap>(20)); } @Override public void startElement(String namespaceUri, String tag, String qName,Attributes attr) throws SAXException { if ("language".equals(tag)) { - version = attr.getValue("version"); + String version = attr.getValue("version"); + versionedResources = new VersionedResources(); + versionedResources.version=version; + versionedResources.loadedFrom=loadedFrom; logger.finest("Version attribute: "+version); } } @@ -234,19 +269,23 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) { return; } if ("language".equals(tag)) { + modulesAll.add(versionedResources); + versionedResources = null; return; } - if (version==null) { + if (versionedResources==null) { return; } - // Store in key map - Map modules = modulesAll.get(modulesAll.size()-1).get(version); - if (modules==null) { - modules = new HashMap(20); - modulesAll.get(modulesAll.size()-1).put(version, modules); + if ("eld-resource".equals(tag)) { + versionedResources.eldResources.add(value); + } else if ("module-loader".equals(tag)) { + versionedResources.moduleLoaders.add(value); + } else if ("elb-resource".equals(tag)) { + versionedResources.elbResources.add(value); + } else if ("sibling-loader".equals(tag)) { + versionedResources.siblingLoaders.add(value); } - modules.put(tag, value); logger.finest("Stored tag: "+tag+" value: "+value); } @@ -259,4 +298,13 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) { buf.append(text); } } + + private class VersionedResources { + public String version; + public String loadedFrom; + public List eldResources = new ArrayList(5); + public List moduleLoaders = new ArrayList(5); + public List elbResources = new ArrayList(5); + public List siblingLoaders = new ArrayList(5); + } } diff --git a/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageModule.java b/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageModule.java index d618bfb..013d1c4 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageModule.java +++ b/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageModule.java @@ -27,7 +27,6 @@ import java.util.List; import org.x4o.xml.element.ElementBindingHandler; import org.x4o.xml.element.ElementConfiguratorGlobal; import org.x4o.xml.element.ElementInterface; -import org.x4o.xml.element.ElementMetaBase; import org.x4o.xml.element.ElementNamespace; /** @@ -37,70 +36,40 @@ import org.x4o.xml.element.ElementNamespace; * @author Willem Cazander * @version 1.0 Aug 2, 2012 */ -public interface X4OLanguageModule extends ElementMetaBase { +public interface X4OLanguageModule /* extends ElementMetaBase TODO add local layer? */ { + // temp here see local + String getId(); + String getDescription(); + /** * @return the providerName. */ String getProviderName(); - - /** - * @param providerName the providerName to set. - */ - void setProviderName(String providerName); /** * @return the providerHost */ String getProviderHost(); - /** - * @param providerHost the providerHost to set - */ - void setProviderHost(String providerHost); - - /** - * Adds an ElementBindingHanlder. - * @param elementBindingHandler The ElementBindingHandler to add. - */ - void addElementBindingHandler(ElementBindingHandler elementBindingHandler); - /** * Gets all ElementBindingHandlers. * @return Returns an List with all ElementBindingHandlers. */ List getElementBindingHandlers(); - /** - * Adds an ElementConfiguratorGlobal. - * @param elementConfigurator The ElementConfigurtor to add. - */ - void addElementConfiguratorGlobal(ElementConfiguratorGlobal elementConfigurator); - /** * Gets all ElementConfiguratorGlobals. * @return All gloval ElementConfigurators. */ List getElementConfiguratorGlobals(); - /** - * Adds an ElementInterface. - * @param elementInterface The elementInterface to add. - */ - void addElementInterface(ElementInterface elementInterface); - /** * Returns list of ElementInterfaces in this context. * @return The list of elementInterfaces. */ List getElementInterfaces(); - /** - * Adds an namespace to this langauge module. - * @param elementNamespace Adds an ElementNamespace to this langauge module. - */ - void addElementNamespace(ElementNamespace elementNamespace); - /** * Returns the namespace context for an namespace uri. * @param namespaceUri the namespace uri. @@ -114,31 +83,9 @@ public interface X4OLanguageModule extends ElementMetaBase { List getElementNamespaces(); /** - * @param elementLanguageModuleLoader Sets the loader of this module. + * Gets module loader meta result info. + * @param key The key to get info of. + * @return The value of the info. */ - void setLanguageModuleLoader(X4OLanguageModuleLoader elementLanguageModuleLoader); - - /** - * @return Returns the ElementLanguageModuleLoader of this module. - */ - X4OLanguageModuleLoader getLanguageModuleLoader(); - - /** - * @return the sourceResource - */ - String getSourceResource(); - - /** - * @param sourceResource the sourceResource to set - */ - void setSourceResource(String sourceResource); - - /* - * - * @param elementLanguage - * @param elementLanguageModule - * @throws ElementLanguageModuleLoaderException - - void reloadModule(ElementLanguage elementLanguage,ElementLanguageModule elementLanguageModule) throws ElementLanguageModuleLoaderException; - */ + String getLoaderResult(X4OLanguageModuleLoaderResult key); } diff --git a/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageModuleLoader.java b/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageModuleLoader.java index 2213d2e..3d3ab22 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageModuleLoader.java +++ b/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageModuleLoader.java @@ -33,9 +33,9 @@ public interface X4OLanguageModuleLoader { /** * Starts the ElementProvider. - * @param languageLocal The ElementLanguage to load for. - * @param elementLanguageModule The ElementLanguageModule to load it into. + * @param language The ElementLanguage to load for. + * @param module The ElementLanguageModule to load it into. * @throws X4OLanguageModuleLoaderException Gets thrown when modules could not be correctly loaded. */ - void loadLanguageModule(X4OLanguageLocal languageLocal,X4OLanguageModule elementLanguageModule) throws X4OLanguageModuleLoaderException; + void loadLanguageModule(X4OLanguageLocal language,X4OLanguageModuleLocal module) throws X4OLanguageModuleLoaderException; } diff --git a/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageModuleLoaderResult.java b/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageModuleLoaderResult.java new file mode 100644 index 0000000..faf601d --- /dev/null +++ b/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageModuleLoaderResult.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2004-2013, Willem Cazander + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.x4o.xml.lang; + +/** + * X4OLanguageModuleLoaderResult are keys of meta info results from the loader which loaded the module. + * + * @author Willem Cazander + * @version 1.0 Mar 7, 2014 + */ +public enum X4OLanguageModuleLoaderResult { + + LOAD_MODULE_RESOURCE, + LOAD_FROM_RESOURCE, + LOAD_VERSION, + LOAD_CLASS, + LOAD_TIME, + LOAD_DATE; +} diff --git a/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageModuleLocal.java b/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageModuleLocal.java new file mode 100644 index 0000000..3caa910 --- /dev/null +++ b/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageModuleLocal.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2004-2013, Willem Cazander + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.x4o.xml.lang; + +import org.x4o.xml.element.ElementBindingHandler; +import org.x4o.xml.element.ElementConfiguratorGlobal; +import org.x4o.xml.element.ElementInterface; +import org.x4o.xml.element.ElementMetaBase; +import org.x4o.xml.element.ElementNamespace; + +/** + * The ElementLanguageModuleLocal is for local loading of the object. + * + * @author Willem Cazander + * @version 1.0 Mar 7, 2014 + */ +public interface X4OLanguageModuleLocal extends X4OLanguageModule,ElementMetaBase { + + /** + * @param providerName the providerName to set. + */ + void setProviderName(String providerName); + + /** + * @param providerHost the providerHost to set + */ + void setProviderHost(String providerHost); + + /** + * Adds an ElementBindingHanlder. + * @param elementBindingHandler The ElementBindingHandler to add. + */ + void addElementBindingHandler(ElementBindingHandler elementBindingHandler); + + /** + * Adds an ElementConfiguratorGlobal. + * @param elementConfigurator The ElementConfigurtor to add. + */ + void addElementConfiguratorGlobal(ElementConfiguratorGlobal elementConfigurator); + + /** + * Adds an ElementInterface. + * @param elementInterface The elementInterface to add. + */ + void addElementInterface(ElementInterface elementInterface); + + /** + * Adds an namespace to this langauge module. + * @param elementNamespace Adds an ElementNamespace to this langauge module. + */ + void addElementNamespace(ElementNamespace elementNamespace); + + /** + * Sets module loader meta result info. + * @param key The key of the info. + * @param value The value of the info. + */ + void putLoaderResult(X4OLanguageModuleLoaderResult key,String value); +} diff --git a/x4o-driver/src/main/java/org/x4o/xml/lang/phase/X4OPhaseLanguageInit.java b/x4o-driver/src/main/java/org/x4o/xml/lang/phase/X4OPhaseLanguageInit.java index 95d856e..06a2e6f 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/lang/phase/X4OPhaseLanguageInit.java +++ b/x4o-driver/src/main/java/org/x4o/xml/lang/phase/X4OPhaseLanguageInit.java @@ -27,7 +27,6 @@ import java.util.List; import java.util.logging.Logger; import org.x4o.xml.element.Element; -import org.x4o.xml.lang.X4OLanguageModule; import org.x4o.xml.lang.X4OLanguageSession; import org.x4o.xml.lang.X4OLanguageModuleLoaderSibling; import org.x4o.xml.lang.X4OLanguageClassLoader; @@ -115,7 +114,12 @@ public class X4OPhaseLanguageInit { /** * Loads all sibling languages. */ - class X4OPhaseInitLanguageSiblings extends AbstractX4OPhase { + public class X4OPhaseInitLanguageSiblings extends AbstractX4OPhase { + private List siblingLoaders = new ArrayList(2); + // mmmm think of better then cast + public void addLanguageModuleLoaderSibling(X4OLanguageModuleLoaderSibling loader) { + siblingLoaders.add(loader); + } public X4OPhaseType getType() { return X4OPhaseType.INIT; } @@ -132,12 +136,6 @@ public class X4OPhaseLanguageInit { } public void runPhase(X4OLanguageSession languageSession) throws X4OPhaseException { try { - List siblingLoaders = new ArrayList(3); - for (X4OLanguageModule module:languageSession.getLanguage().getLanguageModules()) { - if (module.getLanguageModuleLoader() instanceof X4OLanguageModuleLoaderSibling) { - siblingLoaders.add((X4OLanguageModuleLoaderSibling)module.getLanguageModuleLoader()); - } - } if (siblingLoaders.isEmpty()==false) { X4OLanguageLoader loader = (X4OLanguageLoader)X4OLanguageClassLoader.newInstance(languageSession.getLanguage().getLanguageConfiguration().getDefaultLanguageLoader()); for (X4OLanguageModuleLoaderSibling siblingLoader:siblingLoaders) { @@ -147,6 +145,7 @@ public class X4OPhaseLanguageInit { if (languageSession.hasX4ODebugWriter()) { languageSession.getX4ODebugWriter().debugElementLanguageModules(languageSession); } + siblingLoaders.clear(); } } catch (Exception e) { throw new X4OPhaseException(this,e); diff --git a/x4o-driver/src/test/java/org/x4o/xml/lang/DefaultX4OLanguageLoaderTest.java b/x4o-driver/src/test/java/org/x4o/xml/lang/DefaultX4OLanguageLoaderTest.java index 806159f..ae3dae4 100644 --- a/x4o-driver/src/test/java/org/x4o/xml/lang/DefaultX4OLanguageLoaderTest.java +++ b/x4o-driver/src/test/java/org/x4o/xml/lang/DefaultX4OLanguageLoaderTest.java @@ -45,14 +45,14 @@ import junit.framework.TestCase; public class DefaultX4OLanguageLoaderTest extends TestCase { X4ODriver driver; - X4OLanguageLoader loader; + DefaultX4OLanguageLoader loader; public void setUp() throws Exception { driver = TestDriver.getInstance(); //X4OReader reader = driver.createReader(); //reader.readResource("tests/namespace/uri-simple.xml"); X4OLanguage language = driver.createLanguage(); - loader = (X4OLanguageLoader)language.getLanguageConfiguration().getDefaultLanguageLoader().newInstance(); + loader = (DefaultX4OLanguageLoader)language.getLanguageConfiguration().getDefaultLanguageLoader().newInstance(); } @@ -80,6 +80,10 @@ public class DefaultX4OLanguageLoaderTest extends TestCase { loader.loadLanguage((X4OLanguageLocal)result, "test", "1.0"); } + public void testLanguag() throws Exception { +// loader.loadModulesXml(in); + } + /* public void testLanguageURINameSpaceTest() throws Exception { Map languageMap = loader.loadLanguageModules(parser.getElementLanguage(), "test"); diff --git a/x4o-meta/src/main/java/org/x4o/xml/lang/meta/MetaLanguageSiblingLoader.java b/x4o-meta/src/main/java/org/x4o/xml/lang/meta/MetaLanguageSiblingLoader.java index ea3871d..1455299 100644 --- a/x4o-meta/src/main/java/org/x4o/xml/lang/meta/MetaLanguageSiblingLoader.java +++ b/x4o-meta/src/main/java/org/x4o/xml/lang/meta/MetaLanguageSiblingLoader.java @@ -22,12 +22,12 @@ */ package org.x4o.xml.lang.meta; -import org.x4o.xml.lang.X4OLanguageModule; import org.x4o.xml.lang.X4OLanguageModuleLoaderException; import org.x4o.xml.lang.X4OLanguageModuleLoaderSibling; import org.x4o.xml.lang.X4OLanguageLoader; import org.x4o.xml.lang.X4OLanguageLoaderException; import org.x4o.xml.lang.X4OLanguageLocal; +import org.x4o.xml.lang.X4OLanguageModuleLocal; /** * MetaLanguageSiblingLoader loads the generic x4o meta language into defined language. @@ -56,7 +56,7 @@ public class MetaLanguageSiblingLoader implements X4OLanguageModuleLoaderSibling * @throws X4OLanguageModuleLoaderException Is thrown when meta language could not be loaded. * @see org.x4o.xml.lang.X4OLanguageModuleLoader#loadLanguageModule(org.x4o.xml.lang.X4OLanguageLocal, org.x4o.xml.lang.X4OLanguageModule) */ - public void loadLanguageModule(X4OLanguageLocal language,X4OLanguageModule languageModule) throws X4OLanguageModuleLoaderException { + public void loadLanguageModule(X4OLanguageLocal language,X4OLanguageModuleLocal languageModule) throws X4OLanguageModuleLoaderException { languageModule.setId(META_LANGUAGE); languageModule.setProviderHost(META_LANGUAGE_HOST); languageModule.setProviderName(MetaLanguageSiblingLoader.class.getSimpleName()); diff --git a/x4o-meta/src/test/resources/META-INF/mtest/mtest-lang.eld b/x4o-meta/src/test/resources/META-INF/mtest/mtest-lang.eld index d5dec40..6fcdbee 100644 --- a/x4o-meta/src/test/resources/META-INF/mtest/mtest-lang.eld +++ b/x4o-meta/src/test/resources/META-INF/mtest/mtest-lang.eld @@ -29,8 +29,8 @@ xmlns:conv="http://eld.x4o.org/xml/ns/eld-conv" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://eld.x4o.org/xml/ns/eld-root http://eld.x4o.org/xml/ns/eld-root-1.0.xsd" - providerName="mtest.x4o.org" - name="Meta Test Language" + providerHost="mtest.x4o.org" + providerName="Meta Test Language" id="mtest-lang" >