001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1999-2004 Gerald Brose
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Library General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Library General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public
017: * License along with this library; if not, write to the Free
018: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: *
020: */
021:
022: package org.jacorb.notification.jmx;
023:
024: import java.io.IOException;
025: import java.util.Properties;
026:
027: import javax.management.MBeanServer;
028:
029: import org.apache.avalon.framework.logger.Logger;
030: import org.jacorb.notification.AbstractChannelFactory;
031: import org.jacorb.notification.ConsoleMain;
032: import org.jacorb.notification.EventChannelFactoryImpl;
033: import org.jacorb.notification.conf.Attributes;
034: import org.jacorb.notification.util.LogUtil;
035: import org.nanocontainer.remoting.jmx.DynamicMBeanProvider;
036: import org.nanocontainer.remoting.jmx.JMXExposingComponentAdapterFactory;
037: import org.omg.CORBA.IntHolder;
038: import org.omg.CORBA.ORB;
039: import org.omg.CosNotification.Property;
040: import org.picocontainer.MutablePicoContainer;
041: import org.picocontainer.defaults.CachingComponentAdapterFactory;
042: import org.picocontainer.defaults.ComponentAdapterFactory;
043: import org.picocontainer.defaults.ConstructorInjectionComponentAdapterFactory;
044: import org.picocontainer.defaults.DefaultPicoContainer;
045:
046: /**
047: * @jmx.mbean name="COSNotificationServiceMBean"
048: * description="Control the JacORB Notification Service"
049: *
050: * @author Alphonse Bendt
051: * @version $Id: COSNotificationService.java,v 1.4 2005/10/28 10:53:58 alphonse.bendt Exp $
052: */
053: public class COSNotificationService implements
054: COSNotificationServiceMBean {
055: private AbstractChannelFactory factory_;
056:
057: private final MutablePicoContainer container_;
058:
059: private final static String STARTED = "EventChannelFactory was started";
060:
061: private final static String RUNNING = "EventChannelFactory is running";
062:
063: private final static String NOT_RUNNING = "EventChannelFactory is not running";
064:
065: private final static String STOPPED = "EventChannelFactory was stopped";
066:
067: private final static String IOR_DEFAULT = "IOR:0";
068:
069: private final static String CORBALOC_DEFAULT = "<undefined>";
070:
071: private final Logger logger_ = LogUtil.getLogger(getClass()
072: .getName());
073:
074: private final Properties properties_;
075:
076: private final ORB optionalORB_;
077:
078: public COSNotificationService(ORB orb, MBeanServer mbeanServer,
079: DynamicMBeanProvider mbeanProvider, String[] args) {
080: super ();
081:
082: optionalORB_ = orb;
083: properties_ = ConsoleMain.parseProperties(args);
084:
085: DynamicMBeanProvider _decoratedProvider = new UnregisterObjectNameProviderDecorator(
086: mbeanServer, mbeanProvider);
087:
088: ComponentAdapterFactory _defaultCAF = new JMXExposingComponentAdapterFactory(
089: new ConstructorInjectionComponentAdapterFactory(),
090: mbeanServer,
091: new DynamicMBeanProvider[] { _decoratedProvider });
092:
093: ComponentAdapterFactory _cachingCAF = new CachingComponentAdapterFactory(
094: _defaultCAF);
095:
096: container_ = new DefaultPicoContainer(_cachingCAF);
097: container_.registerComponentInstance(
098: ComponentAdapterFactory.class, _defaultCAF);
099: }
100:
101: /**
102: * @jmx.managed-operation description="create a new channel"
103: */
104: public String createChannel() {
105: try {
106: if (factory_ != null) {
107: EventChannelFactoryImpl factory = (EventChannelFactoryImpl) factory_;
108:
109: IntHolder id = new IntHolder();
110:
111: factory.create_channel(new Property[0],
112: new Property[0], id);
113:
114: return "Created Channel id=" + id.value;
115: }
116:
117: return NOT_RUNNING;
118: } catch (Exception e) {
119: logger_.error("Error creating Channel", e);
120:
121: throw new RuntimeException("Create channel failed: "
122: + e.getMessage());
123: }
124: }
125:
126: public String start() {
127: if (factory_ != null) {
128: return RUNNING;
129: }
130:
131: try {
132: factory_ = AbstractChannelFactory.newFactory(optionalORB_,
133: container_, properties_);
134:
135: return STARTED;
136: } catch (Exception e) {
137: logger_.error("Error starting Service", e);
138:
139: throw new RuntimeException("Start failed: "
140: + e.getMessage());
141: }
142: }
143:
144: public String stop() {
145: if (factory_ != null) {
146: factory_.dispose();
147: factory_ = null;
148:
149: return STOPPED;
150: }
151: return NOT_RUNNING;
152: }
153:
154: /**
155: * @jmx.managed-attribute description="IOR to access the EventChannelFactory"
156: * access = "read-only"
157: */
158: public String getIOR() {
159: return (factory_ == null) ? IOR_DEFAULT : factory_.getIOR();
160: }
161:
162: /**
163: * @jmx.managed-attribute description="Corbaloc to access the EventChannelFactory
164: * access = "read-only"
165: */
166: public String getCorbaloc() {
167: return (factory_ == null) ? CORBALOC_DEFAULT : factory_
168: .getCorbaLoc();
169: }
170:
171: /**
172: * @jmx.managed-attribute description = "Filename the IOR should be written to"
173: * access = "read-write"
174: */
175: public String getIORFile() {
176: return properties_.getProperty(Attributes.IOR_FILE);
177: }
178:
179: /**
180: * @jmx.managed-attribute
181: */
182: public void setIORFile(String filename) throws IOException {
183: properties_.setProperty(Attributes.IOR_FILE, filename);
184:
185: if (factory_ != null) {
186: factory_.writeIOR(filename);
187: }
188: }
189:
190: /**
191: * @jmx.managed-attribute description = "NameService Entry (Optional)"
192: * access = "read-write"
193: */
194: public String getCOSNamingEntry() {
195: StringBuffer name = new StringBuffer(properties_.getProperty(
196: Attributes.REGISTER_NAME_ID, "<undefined>"));
197:
198: final String nameKind = properties_
199: .getProperty(Attributes.REGISTER_NAME_KIND);
200: if (nameKind != null) {
201: name.append('.');
202: name.append(nameKind);
203: }
204: return name.toString();
205: }
206:
207: /**
208: * @jmx.managed-attribute
209: */
210: public void setCOSNamingEntry(String registerName) {
211: ConsoleMain.addCOSNamingName(properties_, registerName);
212:
213: if (factory_ != null) {
214: try {
215: factory_.unregisterName();
216: factory_.registerName(properties_);
217: } catch (Exception e) {
218: logger_.error("Error changing COSNaming entry", e);
219:
220: throw new RuntimeException(
221: "Changing the COSNaming entry failed: "
222: + e.getMessage());
223: }
224: }
225: }
226:
227: public org.omg.CORBA.Object getEventChannelFactory() {
228: return factory_.activate();
229: }
230: }
|