001: package org.concern.controller.jmx;
002:
003: import org.concern.*;
004: import org.concern.controller.Worklist;
005: import org.hibernate.SessionFactory;
006:
007: import javax.naming.*;
008: import javax.transaction.TransactionManager;
009: import java.util.*;
010: import java.lang.reflect.Proxy;
011: import java.sql.Timestamp;
012:
013: /**
014: * @see org.concern.controller.jmx.ControllerServiceMBean
015: * @author hengels
016: */
017: public class CollaborationService implements CollaborationServiceMBean {
018: private static final Map collaborations = new HashMap();
019:
020: private String jndiName;
021: private String transactionManagerJndiName;
022: private String sessionFactoryJndiName;
023:
024: private long timerInterval = 10000;
025:
026: static {
027: System.setProperty("concern.controller.lookup",
028: JndiControllerLookup.class.getName());
029: }
030:
031: public String getJndiName() {
032: return jndiName;
033: }
034:
035: public void setJndiName(String jndiName) {
036: this .jndiName = jndiName;
037: }
038:
039: public String getTransactionManagerJndiName() {
040: return transactionManagerJndiName;
041: }
042:
043: public void setTransactionManagerJndiName(
044: String transactionManagerJndiName) {
045: this .transactionManagerJndiName = transactionManagerJndiName;
046: }
047:
048: public String getSessionFactoryJndiName() {
049: return sessionFactoryJndiName;
050: }
051:
052: public void setSessionFactoryJndiName(String sessionFactoryJndiName) {
053: this .sessionFactoryJndiName = sessionFactoryJndiName;
054: }
055:
056: public long getTimerInterval() {
057: return timerInterval;
058: }
059:
060: public void setTimerInterval(long timerInterval) {
061: this .timerInterval = timerInterval;
062: }
063:
064: public void start() throws ControllerException {
065: try {
066: Collaboration collaboration = buildCollaboration();
067: if (jndiName == null)
068: jndiName = "java:controller/Collaboration";
069:
070: CollaborationProxy collaborationProxy = new CollaborationProxy(
071: jndiName);
072: Object proxy = Proxy.newProxyInstance(Thread
073: .currentThread().getContextClassLoader(),
074: new Class[] { org.concern.Collaboration.class },
075: collaborationProxy);
076: CollaborationService.rebind(new InitialContext(), jndiName,
077: proxy);
078: CollaborationService.collaborations.put(jndiName,
079: collaboration);
080: } catch (NamingException e) {
081: e.printStackTrace();
082: }
083: }
084:
085: public void stop() {
086: try {
087: collaborations.remove(jndiName);
088: unbind(new InitialContext(), jndiName);
089: } catch (Exception e) {
090: e.printStackTrace();
091: }
092: }
093:
094: Collaboration buildCollaboration() throws ControllerException {
095: try {
096: return (Collaboration) new org.concern.controller.Collaboration();
097: } catch (Exception e) {
098: throw new ControllerException(e);
099: }
100: }
101:
102: static void rebind(Context context, String string, Object value)
103: throws NamingException {
104: Name name = context.getNameParser("").parse(string);
105: int size = name.size();
106: String atom = name.get(size - 1);
107: Context parentContext = CollaborationService
108: .createChildContext(context, name.getPrefix(size - 1));
109: parentContext.rebind(atom, value);
110: }
111:
112: static Context createChildContext(Context context, Name name)
113: throws NamingException {
114: Context childContext = context;
115: for (int pos = 0; pos < name.size(); pos++) {
116: String string = name.get(pos);
117: try {
118: childContext = (Context) context.lookup(string);
119: } catch (NameNotFoundException e) {
120: childContext = context.createSubcontext(string);
121: }
122: context = childContext;
123: }
124: return childContext;
125: }
126:
127: static void unbind(Context context, String string)
128: throws NamingException {
129: Name name = context.getNameParser("").parse(string);
130: context.unbind(name);
131: /*
132: int size = name.size();
133: while (--size > 0) {
134: Name prefixName = name.getPrefix(size);
135: if (context.listBindings(prefixName).hasMore())
136: break;
137: else
138: context.unbind(prefixName);
139: }
140: */
141: }
142:
143: static Collaboration getCollaboration(String name) {
144: return (Collaboration) CollaborationService.collaborations
145: .get(name);
146: }
147:
148: Collaboration getCollaboration() {
149: return (Collaboration) CollaborationService.collaborations
150: .get(jndiName);
151: }
152:
153: public void register(String name, Callable callable) {
154: getCollaboration().register(name, callable);
155: }
156:
157: public void register(String name, Notifiable notifiable) {
158: getCollaboration().register(name, notifiable);
159: }
160:
161: public void call(Message message) throws RejectCallException {
162: getCollaboration().call(message);
163: }
164:
165: public void notify(Message message) {
166: getCollaboration().notify(message);
167: }
168: }
|