01: /*
02: * JBoss, Home of Professional Open Source
03: * Copyright 2005, JBoss Inc., and individual contributors as indicated
04: * by the @authors tag. See the copyright.txt in the distribution for a
05: * full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jbpm.context.exe;
23:
24: import java.io.InputStream;
25: import java.util.HashMap;
26: import java.util.List;
27: import java.util.Map;
28:
29: import org.jbpm.JbpmConfiguration;
30: import org.jbpm.JbpmException;
31: import org.jbpm.configuration.ObjectFactory;
32: import org.jbpm.configuration.ObjectFactoryParser;
33: import org.jbpm.util.ClassLoaderUtil;
34:
35: /**
36: * specifies for one java-type how jbpm is able to persist objects of that type in the database.
37: */
38: public class JbpmType {
39:
40: static Map jbpmTypesCache = new HashMap();
41:
42: JbpmTypeMatcher jbpmTypeMatcher = null;
43: Converter converter = null;
44: Class variableInstanceClass = null;
45:
46: public JbpmType(JbpmTypeMatcher jbpmTypeMatcher,
47: Converter converter, Class variableInstanceClass) {
48: this .jbpmTypeMatcher = jbpmTypeMatcher;
49: this .converter = converter;
50: this .variableInstanceClass = variableInstanceClass;
51: }
52:
53: public boolean matches(Object value) {
54: return jbpmTypeMatcher.matches(value);
55: }
56:
57: public VariableInstance newVariableInstance() {
58: VariableInstance variableInstance = null;
59: try {
60: variableInstance = (VariableInstance) variableInstanceClass
61: .newInstance();
62: variableInstance.converter = converter;
63: } catch (Exception e) {
64: throw new JbpmException(
65: "couldn't instantiate variable instance class '"
66: + variableInstanceClass.getName() + "'");
67: }
68: return variableInstance;
69: }
70:
71: public static List getJbpmTypes() {
72: List jbpmTypes = null;
73: synchronized (jbpmTypesCache) {
74: ObjectFactory objectFactory = JbpmConfiguration.Configs
75: .getObjectFactory();
76: jbpmTypes = (List) jbpmTypesCache.get(objectFactory);
77: if (jbpmTypes == null) {
78: if (JbpmConfiguration.Configs.hasObject("jbpm.types")) {
79: jbpmTypes = (List) JbpmConfiguration.Configs
80: .getObject("jbpm.types");
81: } else {
82: jbpmTypes = getDefaultJbpmTypes();
83: }
84: jbpmTypesCache.put(objectFactory, jbpmTypes);
85: }
86: }
87: return jbpmTypes;
88: }
89:
90: private static List getDefaultJbpmTypes() {
91: String resource = JbpmConfiguration.Configs
92: .getString("resource.varmapping");
93: InputStream is = ClassLoaderUtil.getStream(resource);
94: ObjectFactory objectFactory = ObjectFactoryParser
95: .parseInputStream(is);
96: return (List) objectFactory.createObject("jbpm.types");
97: }
98: }
|