From 0ed2c1124cf1b9e49a2ec1fa0126a8df09f9e758 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Tue, 7 Oct 2014 09:18:11 +0200 Subject: Repackage to org.cristalise --- .../process/resource/BadArgumentsException.java | 37 ++++ .../resource/DefaultResourceImportHandler.java | 106 +++++++++++ .../kernel/process/resource/Resource.java | 196 +++++++++++++++++++++ .../process/resource/ResourceImportHandler.java | 68 +++++++ .../kernel/process/resource/ResourceLoader.java | 67 +++++++ 5 files changed, 474 insertions(+) create mode 100644 src/main/java/org/cristalise/kernel/process/resource/BadArgumentsException.java create mode 100644 src/main/java/org/cristalise/kernel/process/resource/DefaultResourceImportHandler.java create mode 100644 src/main/java/org/cristalise/kernel/process/resource/Resource.java create mode 100644 src/main/java/org/cristalise/kernel/process/resource/ResourceImportHandler.java create mode 100644 src/main/java/org/cristalise/kernel/process/resource/ResourceLoader.java (limited to 'src/main/java/org/cristalise/kernel/process/resource') diff --git a/src/main/java/org/cristalise/kernel/process/resource/BadArgumentsException.java b/src/main/java/org/cristalise/kernel/process/resource/BadArgumentsException.java new file mode 100644 index 0000000..d8d7dc9 --- /dev/null +++ b/src/main/java/org/cristalise/kernel/process/resource/BadArgumentsException.java @@ -0,0 +1,37 @@ +/** + * This file is part of the CRISTAL-iSE kernel. + * Copyright (c) 2001-2014 The CRISTAL Consortium. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + * + * http://www.fsf.org/licensing/licenses/lgpl.html + */ +package org.cristalise.kernel.process.resource; + +public class BadArgumentsException extends Exception { + + public BadArgumentsException() { + super(); + } + + public BadArgumentsException(String message) { + super(message); + } + + public BadArgumentsException(Throwable cause) { + super(cause); + } + +} diff --git a/src/main/java/org/cristalise/kernel/process/resource/DefaultResourceImportHandler.java b/src/main/java/org/cristalise/kernel/process/resource/DefaultResourceImportHandler.java new file mode 100644 index 0000000..dbd2d44 --- /dev/null +++ b/src/main/java/org/cristalise/kernel/process/resource/DefaultResourceImportHandler.java @@ -0,0 +1,106 @@ +/** + * This file is part of the CRISTAL-iSE kernel. + * Copyright (c) 2001-2014 The CRISTAL Consortium. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + * + * http://www.fsf.org/licensing/licenses/lgpl.html + */ +package org.cristalise.kernel.process.resource; + +import java.util.HashSet; +import java.util.Set; + +import org.cristalise.kernel.lookup.DomainPath; +import org.cristalise.kernel.persistency.outcome.Outcome; +import org.cristalise.kernel.process.Gateway; +import org.cristalise.kernel.property.PropertyDescriptionList; + + +public class DefaultResourceImportHandler implements ResourceImportHandler { + + String schemaName; + String typeRoot; + DomainPath typeRootPath; + String wfDef; + PropertyDescriptionList props; + + public DefaultResourceImportHandler(String resType) throws Exception { + if (resType.equals("CA")) { + wfDef = "ManageCompositeActDef"; + schemaName = "CompositeActivityDef"; + typeRoot = "/desc/ActivityDesc"; + } + else if (resType.equals("EA")) { + wfDef = "ManageElementaryActDef"; + schemaName = "ElementaryActivityDef"; + typeRoot = "/desc/ActivityDesc"; + } + else if (resType.equals("OD")) { + wfDef = "ManageSchema"; + schemaName = "Schema"; + typeRoot = "/desc/OutcomeDesc"; + } + else if (resType.equals("SC")) { + wfDef = "ManageScript"; + schemaName = "Script"; + typeRoot = "/desc/Script"; + } + else if (resType.equals("SM")) { + wfDef = "ManageStateMachine"; + schemaName = "StateMachine"; + typeRoot = "/desc/StateMachine"; + } + else throw new Exception("Unknown bootstrap item type: "+resType); + typeRootPath = new DomainPath(typeRoot); + props = (PropertyDescriptionList)Gateway.getMarshaller().unmarshall(Gateway.getResource().getTextResource(null, "boot/property/"+resType+"Prop.xml")); + } + + @Override + public DomainPath getTypeRoot() { + return typeRootPath; + } + + @Override + public String getName() { + return schemaName; + } + + @Override + public DomainPath getPath(String name, String ns) throws Exception { + return new DomainPath(typeRoot+"/system/"+(ns==null?"kernel":ns)+'/'+name); + } + + @Override + public Set getResourceOutcomes(String name, String ns, String location, int version) throws Exception { + HashSet retArr = new HashSet(); + String data = Gateway.getResource().getTextResource(ns, location); + if (data == null) + throw new Exception("No data found for "+schemaName+" "+name); + Outcome resOutcome = new Outcome(0, data, schemaName, 0); + retArr.add(resOutcome); + return retArr; + } + + @Override + public PropertyDescriptionList getPropDesc() throws Exception { + return props; + } + + @Override + public String getWorkflowName() throws Exception { + return wfDef; + } +} diff --git a/src/main/java/org/cristalise/kernel/process/resource/Resource.java b/src/main/java/org/cristalise/kernel/process/resource/Resource.java new file mode 100644 index 0000000..25b1ee8 --- /dev/null +++ b/src/main/java/org/cristalise/kernel/process/resource/Resource.java @@ -0,0 +1,196 @@ +/** + * This file is part of the CRISTAL-iSE kernel. + * Copyright (c) 2001-2014 The CRISTAL Consortium. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + * + * http://www.fsf.org/licensing/licenses/lgpl.html + */ +package org.cristalise.kernel.process.resource; + +//Java +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Hashtable; + +import org.cristalise.kernel.common.InvalidDataException; +import org.cristalise.kernel.common.ObjectNotFoundException; +import org.cristalise.kernel.utils.FileStringUtility; +import org.cristalise.kernel.utils.Logger; + + +/************************************************************************** + * + * @author $Author: abranson $ $Date: 2005/10/05 07:39:36 $ + * @version $Revision: 1.71 $ + **************************************************************************/ +public class Resource implements ResourceLoader { + + private final Hashtable txtCache = new Hashtable(); + private final URL baseURL; + private final HashMap moduleBaseURLs = new HashMap(); + private final HashMap allBaseURLs = new HashMap(); + + public Resource() throws InvalidDataException { + baseURL = getURLorResURL("org/cristalise/kernel/utils/resources/"); + allBaseURLs.put(null, baseURL); + } + + /* (non-Javadoc) + * @see org.cristalise.kernel.utils.ResourceLoader#getKernelBaseURL() + */ + @Override + public URL getKernelBaseURL() { + return baseURL; + } + + @Override + public ClassLoader getClassLoader(String className) { + return Resource.class.getClassLoader(); + } + + @Override + public Class getClassForName(String name) throws ClassNotFoundException { + return Class.forName(name); + } + + /* (non-Javadoc) + * @see org.cristalise.kernel.utils.ResourceLoader#getKernelResourceURL(java.lang.String) + */ + @Override + public URL getKernelResourceURL(String resName) throws MalformedURLException { + return new URL(baseURL, resName); + } + + /* (non-Javadoc) + * @see org.cristalise.kernel.utils.ResourceLoader#addModuleBaseURL(java.lang.String, java.net.URL) + */ + @Override + public void addModuleBaseURL(String ns, URL newBaseURL) { + moduleBaseURLs.put(ns, newBaseURL); + allBaseURLs.put(ns, newBaseURL); + Logger.msg("Adding resource URL for "+ns+": "+newBaseURL.toString()); + } + + /* (non-Javadoc) + * @see org.cristalise.kernel.utils.ResourceLoader#addModuleBaseURL(java.lang.String, java.lang.String) + */ + @Override + public void addModuleBaseURL(String ns, String newBaseURL) throws InvalidDataException { + addModuleBaseURL(ns, getURLorResURL(newBaseURL)); + } + + /* (non-Javadoc) + * @see org.cristalise.kernel.utils.ResourceLoader#getModuleBaseURLs() + */ + @Override + public HashMap getModuleBaseURLs() { + return moduleBaseURLs; + } + + private HashMap getAllBaseURLs() { + return allBaseURLs; + } + + /* (non-Javadoc) + * @see org.cristalise.kernel.utils.ResourceLoader#getModuleResourceURL(java.lang.String, java.lang.String) + */ + @Override + public URL getModuleResourceURL(String ns, String resName) throws MalformedURLException { + return new URL(moduleBaseURLs.get(ns), resName); + } + + static private URL getURLorResURL(String newURL) throws InvalidDataException { + URL result; + try { + result = new URL(newURL); + } catch (java.net.MalformedURLException ex) { + //it is assumed that a 'wrong' URL denotes a directory + //of files resolvable from the CLASSPATH + result = Resource.class.getClassLoader().getResource(newURL); + } + if (result == null) { + Logger.error("URL "+newURL+" could not be found"); + throw new InvalidDataException(); + } + return result; + } + /* (non-Javadoc) + * @see org.cristalise.kernel.utils.ResourceLoader#findTextResource(java.lang.String) + */ + + @Override + public String findTextResource(String resName) { + for (String ns : getAllBaseURLs().keySet()) { + try { + return getTextResource(ns, resName); + } catch (ObjectNotFoundException ex) { } + } + Logger.warning("Text resource '"+resName+"' not found."); + return null; + } + + /* (non-Javadoc) + * @see org.cristalise.kernel.utils.ResourceLoader#getAllTextResources(java.lang.String) + */ + @Override + public HashMap getAllTextResources(String resName) { + HashMap results = new HashMap(); + for (String ns : getAllBaseURLs().keySet()) { + try { + results.put(ns, getTextResource(ns, resName)); + } catch (ObjectNotFoundException ex) { } + } + return results; + } + + /* (non-Javadoc) + * @see org.cristalise.kernel.utils.ResourceLoader#getTextResource(java.lang.String, java.lang.String) + */ + @Override + public String getTextResource(String ns, String resName) throws ObjectNotFoundException + // throws IOException + { + Logger.msg(8, "Resource::getTextResource() - Getting resource from "+ns+": " + resName); + + if (txtCache.containsKey(ns+'/'+resName)) { + return txtCache.get(ns+'/'+resName); + } + + try { + + String newRes = null; + URL loc; + + if (ns == null) // kernel + loc = getKernelResourceURL(resName); + else + loc = getModuleResourceURL(ns, resName); + Logger.msg(5, "Loading resource: "+loc.toString()); + newRes = FileStringUtility.url2String(loc); + txtCache.put(ns+'/'+resName, newRes); + return newRes; + } catch (Exception e) { + throw new ObjectNotFoundException(e.getMessage()); + } + } + + @Override + public Enumeration getModuleDefURLs() throws Exception { + return ClassLoader.getSystemResources("META-INF/cristal/module.xml"); + } +} diff --git a/src/main/java/org/cristalise/kernel/process/resource/ResourceImportHandler.java b/src/main/java/org/cristalise/kernel/process/resource/ResourceImportHandler.java new file mode 100644 index 0000000..9d37e31 --- /dev/null +++ b/src/main/java/org/cristalise/kernel/process/resource/ResourceImportHandler.java @@ -0,0 +1,68 @@ +/** + * This file is part of the CRISTAL-iSE kernel. + * Copyright (c) 2001-2014 The CRISTAL Consortium. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + * + * http://www.fsf.org/licensing/licenses/lgpl.html + */ +package org.cristalise.kernel.process.resource; + +import java.util.Set; + +import org.cristalise.kernel.lookup.DomainPath; +import org.cristalise.kernel.persistency.outcome.Outcome; +import org.cristalise.kernel.property.PropertyDescriptionList; + + +public interface ResourceImportHandler { + + + /** Returns the DomainPath for a specific resource + * @param ns - module namespace + * @param name - resource name + * @return + */ + public DomainPath getPath(String name, String ns) throws Exception; + + /** Generates the outcomes that the resource should contain. + * @param res - the module resource definition + * @return a set of outcomes to be synced with the resource item. + * @throws Exception - if the supplied resources are not valid + */ + public Set getResourceOutcomes(String name, String ns, String location, int version) throws Exception; + + /** Gives the CompActDef name to instantiate to provide the workflow for this type of resource. + * Should be found in the CA typeroot (/desc/ActivityDesc/) + * @return String workflow name + * @throws Exception + */ + public String getWorkflowName() throws Exception; + + /** Should return all of the Properties the resource Item + * will have on creation. The Property 'Name' will be created and populated automatically, even if not declared. + * @return a PropertyDescriptionList - an arraylist of PropertyDescriptions + * @throws Exception + */ + public PropertyDescriptionList getPropDesc() throws Exception; + + /** The directory context to search for existing resources. The name of the resource must be unique below this point. + * @return Root path + */ + public DomainPath getTypeRoot(); + + public String getName(); + +} diff --git a/src/main/java/org/cristalise/kernel/process/resource/ResourceLoader.java b/src/main/java/org/cristalise/kernel/process/resource/ResourceLoader.java new file mode 100644 index 0000000..4ef0e9d --- /dev/null +++ b/src/main/java/org/cristalise/kernel/process/resource/ResourceLoader.java @@ -0,0 +1,67 @@ +/** + * This file is part of the CRISTAL-iSE kernel. + * Copyright (c) 2001-2014 The CRISTAL Consortium. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + * + * http://www.fsf.org/licensing/licenses/lgpl.html + */ +package org.cristalise.kernel.process.resource; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Enumeration; +import java.util.HashMap; + +import org.cristalise.kernel.common.InvalidDataException; +import org.cristalise.kernel.common.ObjectNotFoundException; + + +public interface ResourceLoader { + + public URL getKernelBaseURL(); + + public URL getKernelResourceURL(String resName) + throws MalformedURLException; + + public void addModuleBaseURL(String ns, URL newBaseURL); + + public void addModuleBaseURL(String ns, String newBaseURL) + throws InvalidDataException; + + public HashMap getModuleBaseURLs(); + + public URL getModuleResourceURL(String ns, String resName) + throws MalformedURLException; + + /************************************************************************** + * Gets any text resource files + **************************************************************************/ + + public String findTextResource(String resName); + + public HashMap getAllTextResources(String resName); + + public String getTextResource(String ns, String resName) + throws ObjectNotFoundException; + + public Class getClassForName(String name) + throws ClassNotFoundException; + + public ClassLoader getClassLoader(String className); + + public Enumeration getModuleDefURLs() throws Exception; + +} \ No newline at end of file -- cgit v1.2.3