001: /*
002: * $Id: ControllerService.java 881 2007-02-12 07:54:31Z hengels $
003: * (c) Copyright 2004 con:cern development team.
004: *
005: * This file is part of con:cern (http://concern.org).
006: *
007: * con:cern is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU Lesser General Public License
009: * as published by the Free Software Foundation; either version 2.1
010: * of the License, or (at your option) any later version.
011: *
012: * Please see COPYING for the complete licence.
013: */
014: package org.concern.controller.jmx;
015:
016: import org.hibernate.SessionFactory;
017: import org.concern.*;
018: import org.concern.Controller;
019: import org.concern.Log;
020: import org.concern.controller.*;
021:
022: import javax.naming.*;
023: import javax.transaction.TransactionManager;
024: import java.lang.reflect.Proxy;
025: import java.util.HashMap;
026: import java.util.List;
027: import java.util.Map;
028: import java.io.*;
029:
030: /**
031: * @see ControllerServiceMBean
032: * @author hengels
033: */
034: public class ControllerService implements ControllerServiceMBean {
035: private static final Map<String, Controller> controllers = new HashMap<String, Controller>();
036:
037: private String resourceLocatorClassName = JndiResourceLocator.class
038: .getName();
039: private String parameterResolverClassName = NoParameterResolver.class
040: .getName();
041: private String jndiName;
042: private String transactionManagerJndiName;
043: private String sessionFactoryJndiName;
044: private String processDescription;
045:
046: private long timerInterval = 10000;
047: protected Configuration configuration;
048:
049: static {
050: System.setProperty("concern.controller.lookup",
051: JndiControllerLookup.class.getName());
052: }
053:
054: public String getJndiName() {
055: return jndiName;
056: }
057:
058: public void setJndiName(String jndiName) {
059: this .jndiName = jndiName;
060: }
061:
062: public String getTransactionManagerJndiName() {
063: return transactionManagerJndiName;
064: }
065:
066: public void setTransactionManagerJndiName(
067: String transactionManagerJndiName) {
068: this .transactionManagerJndiName = transactionManagerJndiName;
069: }
070:
071: public String getSessionFactoryJndiName() {
072: return sessionFactoryJndiName;
073: }
074:
075: public void setSessionFactoryJndiName(String sessionFactoryJndiName) {
076: this .sessionFactoryJndiName = sessionFactoryJndiName;
077: }
078:
079: public long getTimerInterval() {
080: return timerInterval;
081: }
082:
083: public void setTimerInterval(long timerInterval) {
084: this .timerInterval = timerInterval;
085: }
086:
087: public void setResourceLocatorClassName(String name) {
088: this .resourceLocatorClassName = name;
089: }
090:
091: public String getResourceLocatorClassName() {
092: return resourceLocatorClassName;
093: }
094:
095: public String getParameterResolverClassName() {
096: return parameterResolverClassName;
097: }
098:
099: public void setParameterResolverClassName(
100: String parameterResolverClassName) {
101: this .parameterResolverClassName = parameterResolverClassName;
102: }
103:
104: public String getProcessDescription() {
105: return processDescription;
106: }
107:
108: public void setProcessDescription(String processDescription) {
109: this .processDescription = processDescription;
110: }
111:
112: private InputStream getProcessDescriptionAsStream() {
113: if (processDescription != null)
114: try {
115: return new ByteArrayInputStream(processDescription
116: .getBytes("UTF-8"));
117: } catch (UnsupportedEncodingException e) {
118: throw new RuntimeException(e);
119: }
120: else
121: return Thread
122: .currentThread()
123: .getContextClassLoader()
124: .getResourceAsStream("META-INF/concern-process.cpd");
125: }
126:
127: public void start() throws ControllerException {
128: try {
129: Controller controller = buildController();
130: if (jndiName == null)
131: jndiName = "java:controller/"
132: + controller.getProcessName();
133:
134: ControllerProxy controllerProxy = new ControllerProxy(
135: jndiName);
136: Object proxy = Proxy.newProxyInstance(Thread
137: .currentThread().getContextClassLoader(),
138: new Class[] { Controller.class }, controllerProxy);
139: rebind(new InitialContext(), jndiName, proxy);
140: controllers.put(jndiName, controller);
141: controller.start();
142: } catch (NamingException e) {
143: e.printStackTrace();
144: }
145: }
146:
147: public void stop() {
148: try {
149: Controller controller = controllers.remove(jndiName);
150: controller.stop();
151: unbind(new InitialContext(), jndiName);
152: } catch (Exception e) {
153: e.printStackTrace();
154: }
155: }
156:
157: Controller buildController() throws ControllerException {
158: try {
159: configuration = new Configuration();
160: configuration.setClassLoader(Thread.currentThread()
161: .getContextClassLoader());
162: configuration.setProcess(Configuration
163: .loadProcess(getProcessDescriptionAsStream()));
164: configuration
165: .setTransactionManager((TransactionManager) new InitialContext()
166: .lookup(transactionManagerJndiName));
167: configuration
168: .setSessionFactory((SessionFactory) new InitialContext()
169: .lookup(sessionFactoryJndiName));
170: configuration.setResourceLocator((ResourceLocator) Class
171: .forName(resourceLocatorClassName).newInstance());
172: configuration
173: .setParameterResolver((ParameterResolver) Class
174: .forName(parameterResolverClassName)
175: .newInstance());
176: configuration.setTimerInterval(timerInterval);
177:
178: return configuration.buildController();
179: } catch (Exception e) {
180: throw new ControllerException(e);
181: }
182: }
183:
184: static void rebind(Context context, String string, Object value)
185: throws NamingException {
186: Name name = context.getNameParser("").parse(string);
187: int size = name.size();
188: String atom = name.get(size - 1);
189: Context parentContext = createChildContext(context, name
190: .getPrefix(size - 1));
191: parentContext.rebind(atom, value);
192: }
193:
194: static Context createChildContext(Context context, Name name)
195: throws NamingException {
196: Context childContext = context;
197: for (int pos = 0; pos < name.size(); pos++) {
198: String string = name.get(pos);
199: try {
200: childContext = (Context) context.lookup(string);
201: } catch (NameNotFoundException e) {
202: childContext = context.createSubcontext(string);
203: }
204: context = childContext;
205: }
206: return childContext;
207: }
208:
209: static void unbind(Context context, String string)
210: throws NamingException {
211: Name name = context.getNameParser("").parse(string);
212: context.unbind(name);
213: /*
214: int size = name.size();
215: while (--size > 0) {
216: Name prefixName = name.getPrefix(size);
217: if (context.listBindings(prefixName).hasMore())
218: break;
219: else
220: context.unbind(prefixName);
221: }
222: */
223: }
224:
225: static Controller getController(String name) {
226: return controllers.get(name);
227: }
228:
229: Controller getController() {
230: return controllers.get(jndiName);
231: }
232:
233: public String getProcessName() {
234: return getController().getProcessName();
235: }
236:
237: public Integer createSubject(String userValue)
238: throws SubjectCreationException {
239: return getController().createSubject(userValue);
240: }
241:
242: public boolean isKnownSubject(String userValue)
243: throws ControllerException {
244: return getController().isKnownSubject(userValue);
245: }
246:
247: public List<String> getSubjects(int state) {
248: return getController().getSubjects(state);
249: }
250:
251: public void announceSubject(String userValue)
252: throws UnknownSubjectException {
253: getController().announceSubject(userValue);
254: }
255:
256: public void scheduleAnnouncement(String userValue, long duration)
257: throws UnknownSubjectException {
258: getController().scheduleAnnouncement(userValue, duration);
259: }
260:
261: public void log(Log log) throws UnknownSubjectException {
262: getController().log(log);
263: }
264:
265: public void lockSubject(String userValue)
266: throws UnknownSubjectException, ControllerException {
267: getController().lockSubject(userValue);
268: }
269:
270: public boolean isForwardPossible(String userValue,
271: String activityName) throws UnknownSubjectException,
272: ControllerException {
273: return getController().isForwardPossible(userValue,
274: activityName);
275: }
276:
277: public void forward(String userValue, String activityName)
278: throws UnknownSubjectException, ControllerException {
279: getController().forward(userValue, activityName);
280: }
281:
282: public void forward(String userValue, String activityName,
283: Map<String, String> logAnnotations)
284: throws UnknownSubjectException, ControllerException {
285: getController()
286: .forward(userValue, activityName, logAnnotations);
287: }
288:
289: public boolean isBackwardPossible(String userValue,
290: String activityName) throws UnknownSubjectException,
291: ControllerException {
292: return getController().isBackwardPossible(userValue,
293: activityName);
294: }
295:
296: public void backward(String userValue, String activityName)
297: throws UnknownSubjectException, ControllerException {
298: getController().backward(userValue, activityName);
299: }
300:
301: public void backward(String userValue, String activityName,
302: Map<String, String> logAnnotations)
303: throws UnknownSubjectException, ControllerException {
304: getController().backward(userValue, activityName,
305: logAnnotations);
306: }
307:
308: public void reset(String userValue, String activityName)
309: throws UnknownSubjectException, ControllerException {
310: getController().reset(userValue, activityName);
311: }
312:
313: public void suspendSubject(String userValue)
314: throws UnknownSubjectException, ControllerException {
315: getController().suspendSubject(userValue);
316: }
317:
318: public void resumeSubject(String userValue)
319: throws UnknownSubjectException, ControllerException {
320: getController().resumeSubject(userValue);
321: }
322:
323: public org.concern.model.Process getProcess() {
324: return getController().getProcess();
325: }
326:
327: public List<String> getEnlistments(String userValue)
328: throws UnknownSubjectException, ControllerException {
329: return getController().getEnlistments(userValue);
330: }
331:
332: public List<String> getTasks(String userValue)
333: throws UnknownSubjectException, ControllerException {
334: return getController().getTasks(userValue);
335: }
336:
337: public List<String> getOptions(String userValue)
338: throws UnknownSubjectException, ControllerException {
339: return getController().getOptions(userValue);
340: }
341:
342: public List<String> getSubjects(String activity) {
343: return getController().getSubjects(activity);
344: }
345:
346: public List<Log> getLog(String userValue)
347: throws UnknownSubjectException {
348: return getController().getLog(userValue);
349: }
350:
351: public List<Log> getArchive(String userValue)
352: throws ControllerException, UnknownSubjectException {
353: return getController().getArchive(userValue);
354: }
355:
356: public boolean complete(String userValue, String activityName)
357: throws UnknownSubjectException, ControllerException {
358: return getController().complete(userValue, activityName);
359: }
360:
361: public boolean complete(String userValue, String activityName,
362: Map<String, String> logAnnotations)
363: throws UnknownSubjectException, ControllerException {
364: return getController().complete(userValue, activityName,
365: logAnnotations);
366: }
367:
368: public Boolean matchCondition(String userValue, String conditionName)
369: throws UnknownSubjectException, ControllerException {
370: return getController().matchCondition(userValue, conditionName);
371: }
372:
373: public boolean matchPrecondition(String userValue,
374: String activityName) throws UnknownSubjectException,
375: ControllerException {
376: return getController().matchPrecondition(userValue,
377: activityName);
378: }
379:
380: public boolean matchPostcondition(String userValue,
381: String activityName) throws UnknownSubjectException,
382: ControllerException {
383: return getController().matchPostcondition(userValue,
384: activityName);
385: }
386:
387: public void process(String userValue)
388: throws UnknownSubjectException, ControllerException {
389: getController().process(userValue);
390: }
391:
392: public void destroySubject(String userValue)
393: throws UnknownSubjectException {
394: getController().destroySubject(userValue);
395: }
396:
397: public void reviveSubject(String userValue)
398: throws UnknownSubjectException, ControllerException {
399: getController().reviveSubject(userValue);
400: }
401:
402: public boolean notify(String userValue, String eventName)
403: throws UnknownSubjectException {
404: return getController().notify(userValue, eventName);
405: }
406:
407: public boolean notify(String userValue, String eventName,
408: Map<String, String> logAnnotations)
409: throws UnknownSubjectException, ControllerException {
410: return getController().notify(userValue, eventName,
411: logAnnotations);
412: }
413:
414: public void reload() {
415: getController().reload();
416: }
417: }
|