001: /*--
002:
003: Copyright (C) 2002-2005 Adrian Price.
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: 1. Redistributions of source code must retain the above copyright
011: notice, this list of conditions, and the following disclaimer.
012:
013: 2. Redistributions in binary form must reproduce the above copyright
014: notice, this list of conditions, and the disclaimer that follows
015: these conditions in the documentation and/or other materials
016: provided with the distribution.
017:
018: 3. The names "OBE" and "Open Business Engine" must not be used to
019: endorse or promote products derived from this software without prior
020: written permission. For written permission, please contact
021: adrianprice@sourceforge.net.
022:
023: 4. Products derived from this software may not be called "OBE" or
024: "Open Business Engine", nor may "OBE" or "Open Business Engine"
025: appear in their name, without prior written permission from
026: Adrian Price (adrianprice@users.sourceforge.net).
027:
028: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
029: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
030: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
031: DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
032: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
033: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
034: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
035: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
036: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
037: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
038: POSSIBILITY OF SUCH DAMAGE.
039:
040: For more information on OBE, please see
041: <http://obe.sourceforge.net/>.
042:
043: */
044:
045: package org.obe.runtime.tool;
046:
047: import org.apache.commons.logging.Log;
048: import org.apache.commons.logging.LogFactory;
049: import org.obe.client.api.repository.ObjectNotFoundException;
050: import org.obe.client.api.repository.RepositoryException;
051: import org.obe.client.api.repository.ToolAgentMetaData;
052: import org.obe.client.api.tool.Parameter;
053: import org.obe.client.api.tool.ToolAgent;
054: import org.obe.engine.EngineContext;
055: import org.obe.engine.repository.AbstractRepository;
056: import org.obe.spi.service.ServiceManager;
057: import org.obe.spi.service.ToolAgentFactory;
058: import org.obe.xpdl.model.application.Application;
059: import org.obe.xpdl.model.data.ExternalReference;
060: import org.xml.sax.EntityResolver;
061:
062: import java.io.IOException;
063: import java.util.Locale;
064:
065: /**
066: * Standard implementation of a tool agent factory. The tool repository is used
067: * to retrieve applications and procedures at runtime. Procedures are pieces of
068: * code which are executed directly in the workflow engine, whereas applications
069: * are executed by worklist handlers on behalf of users.
070: *
071: * @author Adrian Price
072: */
073: public class BasicToolAgentFactory extends AbstractRepository implements
074: ToolAgentFactory {
075:
076: private static final Log _logger = LogFactory
077: .getLog(BasicToolAgentFactory.class);
078:
079: private static void initializeMetaData(ToolAgentMetaData[] metaData) {
080: for (int i = 0; i < metaData.length; i++)
081: metaData[i].init();
082: }
083:
084: public BasicToolAgentFactory(ServiceManager svcMgr) {
085: super (svcMgr, ToolAgentMetaData.class);
086: }
087:
088: public synchronized void init() throws IOException,
089: RepositoryException {
090: super .init();
091: initializeMetaData(findToolTypes(Locale.getDefault().toString()));
092: initializeMetaData(findToolMetaData());
093: }
094:
095: public void createTool(ToolAgentMetaData tool)
096: throws RepositoryException {
097: createEntry(tool);
098: }
099:
100: public void deleteTool(String id) throws RepositoryException {
101: deleteEntry(id);
102: }
103:
104: public void updateTool(ToolAgentMetaData tool)
105: throws RepositoryException {
106: updateEntry(tool.getId(), tool);
107: }
108:
109: public ToolAgentMetaData[] findToolTypes(String locale)
110: throws RepositoryException {
111:
112: return (ToolAgentMetaData[]) findObjectTypes();
113: }
114:
115: public ToolAgentMetaData findToolType(String className,
116: String locale) throws RepositoryException {
117:
118: return (ToolAgentMetaData) findObjectType(className);
119: }
120:
121: public ToolAgentMetaData[] findToolMetaData()
122: throws RepositoryException {
123: return (ToolAgentMetaData[]) findMetaData();
124: }
125:
126: public ToolAgentMetaData findToolMetaData(Application toolDef)
127: throws RepositoryException {
128:
129: return (ToolAgentMetaData) findEntry(toolDef).getMetaData();
130: }
131:
132: public ToolAgent findToolAgent(Application toolDef,
133: Parameter[] parameters) throws RepositoryException {
134:
135: return (ToolAgent) findEntry(toolDef).getInstance(
136: _svcMgr.getResourceRepository(), parameters);
137: }
138:
139: private Entry findEntry(Application toolDef)
140: throws RepositoryException {
141: ExternalReference extRef = toolDef.getExternalReference();
142: boolean useExtRef = extRef != null;
143: String toolKey = useExtRef ? extRef.toString() : toolDef
144: .getId();
145:
146: // If the tool's invocation interface is defined by XPDL
147: // FormalParameters, the tool must already have matching meta-data in
148: // the tool agent factory.
149: Entry entry = findEntry(toolKey, !useExtRef);
150:
151: // If we didn't find suitably configured tool metadata for this external
152: // reference, we must determine whether there is a registered tool type
153: // which can handle this external reference. Do this by offering the
154: // external reference to each registered tool type in turn.
155: if (entry == null) {
156: ToolAgentMetaData[] types = findToolTypes(null);
157: for (int i = 0; i < types.length; i++) {
158: ToolAgentMetaData type = types[i];
159: EntityResolver entityResolver = EngineContext
160: .peekContext().getServiceManager()
161: .getResourceRepository();
162: if ((type = type.introspect(extRef, entityResolver)) != null) {
163: entry = createEntry(toolKey, type, null);
164: break;
165: }
166: }
167: if (entry == null) {
168: throw new ObjectNotFoundException(
169: "No tool type registered for " + toolKey);
170: }
171: }
172: return entry;
173: }
174:
175: protected Log getLogger() {
176: return _logger;
177: }
178:
179: public String getServiceName() {
180: return SERVICE_NAME;
181: }
182: }
|