001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)ProxyBindingLifeCycle.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.binding.proxy;
030:
031: import java.io.File;
032: import java.util.Date;
033:
034: import com.sun.jbi.binding.proxy.util.Translator;
035: import com.sun.jbi.util.jmx.MBeanUtils;
036: import com.sun.jbi.component.ComponentContext;
037:
038: import com.sun.jbi.management.MBeanNames;
039: import com.sun.jbi.management.support.MBeanNamesImpl;
040: import com.sun.jbi.management.support.JbiNameInfo;
041:
042: import com.sun.jbi.messaging.DeliveryChannel;
043:
044: import java.util.logging.Logger;
045: import java.util.Random;
046:
047: import javax.jbi.messaging.InOnly;
048: import javax.jbi.messaging.MessageExchange;
049: import javax.jbi.messaging.MessagingException;
050:
051: import javax.jbi.servicedesc.ServiceEndpoint;
052:
053: import javax.management.MBeanNotificationInfo;
054: import javax.management.MBeanServer;
055: import javax.management.Notification;
056: import javax.management.ObjectName;
057: import javax.management.StandardMBean;
058:
059: import javax.xml.namespace.QName;
060:
061: import org.w3c.dom.Document;
062: import org.w3c.dom.DocumentFragment;
063:
064: /**
065: * This is the Lifecycle class of the Proxy Binding. The init and the start()
066: * methods of this class are invoked by the framework when the component is
067: * started
068: *
069: * @author Sun Microsystems, Inc.
070: */
071: public class ProxyBindingLifeCycle extends
072: com.sun.jbi.management.binding.ModelBindingComponent implements
073: javax.jbi.component.ComponentLifeCycle,
074: javax.jbi.component.Component {
075: /**
076: * Binding context.
077: */
078: private ComponentContext mContext = null;
079:
080: /**
081: * ProxyBinding instance
082: */
083: private ProxyBinding mProxyBinding;
084:
085: /**
086: * Logger Object
087: */
088: private Logger mLog;
089:
090: /**
091: * Our immutable name
092: */
093: JbiNameInfo mJbiNameInfo;
094:
095: /**
096: * Our JbiInstanceName.
097: */
098: String mInstanceName;
099:
100: /**
101: * Flag to hold the result of init
102: */
103: private boolean mInitSuccess = false;
104:
105: int mNotificationSequence;
106:
107: /**
108: * handle to the global statistics MBean
109: */
110: private ProxyBindingStatistics mProxyBindingStatistics;
111:
112: /**
113: * handle to the MBean server
114: */
115: MBeanServer mMBeanServer;
116:
117: // reference to MBeanNames
118: private final MBeanNames mBeanNamesImpl = new MBeanNamesImpl(
119: "com.sun.jbi", System
120: .getProperty("com.sun.jbi.instanceName"));
121:
122: //
123: //
124: // ---------- Methods defined in javax.jbi.component.ComponentLifeCycle ---------
125: //
126:
127: /**
128: * Get the ObjectName for any MBean that is provided for managing this
129: * binding. In this case, there is none, so a null is returned.
130: * @return ObjectName is always null.
131: */
132: public javax.management.ObjectName getExtensionMBeanName() {
133: return null;
134: }
135:
136: /**
137: * Initialize the Proxy BC. This performs initialization required by the
138: * Proxy BC but does not make it ready to process messages. This method is
139: * called immediately after installation of the Proxy BC. It is also
140: * called when the JBI framework is starting up, and any time the BC is
141: * being restarted after previously being shut down through a call to
142: * shutdown().
143: * @param jbiContext the JBI environment context
144: * @throws javax.jbi.JBIException if the BC is unable to initialize.
145: */
146: public void init(javax.jbi.component.ComponentContext jbiContext)
147: throws javax.jbi.JBIException {
148: //initialize our immutable name:
149: mJbiNameInfo = new JbiNameInfo(jbiContext.getComponentName(),
150: true);
151:
152: //we are a model component - intialize the model:
153: super .initModelBindingComponent(jbiContext, mJbiNameInfo);
154:
155: super .bootstrap();
156:
157: mInstanceName = super .mMBeanNames.getJbiInstanceName();
158: mContext = (ComponentContext) jbiContext;
159: mLog = mContext.getLogger("lifecycle", null);
160: Translator.setStringTranslator(mContext
161: .getStringTranslator("com.sun.jbi.binding.proxy"));
162: mLog.info(Translator.translate(LocalStringKeys.INIT_START));
163:
164: // get a handle to the MBEan Server
165: mMBeanServer = (MBeanServer) mContext.getMBeanServer();
166:
167: // seed MBeanUtils with the MBean Server
168: //MBeanUtils.setMBeanServer(mMBeanServer);
169: MBeanUtils.init(mMBeanServer);
170:
171: // init the statistics MBean
172: registerGlobalStatsMBean();
173:
174: /* This a guard variable to check if init is a success
175: * Nothing should be done in start in case init is a failure
176: */
177: mInitSuccess = true;
178: }
179:
180: /**
181: * Shutdown the BC. This performs cleanup before the BC is terminated.
182: * Once this has been called, init() must be called before the BC can be
183: * started again with a call to start().
184: * @throws javax.jbi.JBIException if the BC is unable to shut down.
185: */
186: public void shutDown() throws javax.jbi.JBIException {
187: mLog.info(Translator.translate(LocalStringKeys.SHUTDOWN_BEGIN));
188: mLog.info(Translator.translate(LocalStringKeys.SHUTDOWN_END));
189:
190: // reset all the Statistics
191: // and unregister the MBeans
192: mLog.info(Translator
193: .translate(LocalStringKeys.STATS_MBEAN_RESET));
194:
195: }
196:
197: /**
198: * Start the Proxy BC. This makes the Proxy BC ready to process messages.
199: * This method is called after init() completes when the JBI framework is
200: * starting up, and when the BC is being restarted after a previous call
201: * to shutdown(). If stop() was called previously but shutdown() was not,
202: * start() can be called without a call to init().
203: * @throws javax.jbi.JBIException if the BC is unable to start.
204: */
205: public void start() throws javax.jbi.JBIException {
206: mLog.info(Translator.translate(LocalStringKeys.START_BEGIN,
207: mInstanceName));
208:
209: /* check if init is success */
210: if (!mInitSuccess) {
211: mLog.severe(Translator
212: .translate(LocalStringKeys.INIT_FAILED));
213: return;
214: }
215:
216: try {
217: mProxyBinding = new ProxyBinding(this , mInstanceName);
218: mProxyBinding.start();
219:
220: // set the Proxy Binding restart time
221: mProxyBindingStatistics.setNodeRestartTime(new Date());
222: //mChannel.setResolver(mResolver);
223: } catch (MessagingException me) {
224: mLog.severe(Translator
225: .translate(LocalStringKeys.DELIVERYCHANNEL_FAILED)
226: + me.getMessage());
227: return;
228: }
229:
230: mLog.info(Translator.translate(LocalStringKeys.START_END));
231: }
232:
233: /**
234: * Stop the BC. This makes the BC stop accepting messages for processing.
235: * After a call to this method, start() can be called again without first
236: * calling init().
237: * @throws javax.jbi.JBIException if the BC is unable to stop.
238: */
239: public void stop() throws javax.jbi.JBIException {
240: mLog.info(Translator.translate(LocalStringKeys.STOP_BEGIN,
241: mInstanceName));
242:
243: try {
244: if (mProxyBinding != null) {
245: // remove all stats MBeans
246: unregisterGlobalStatsMBean();
247: mProxyBinding.unregisterAllStatsMBeans();
248: mProxyBinding.stop();
249: mProxyBinding = null;
250: }
251: } catch (Exception e) {
252: e.printStackTrace();
253: }
254:
255: mLog.info(Translator.translate(LocalStringKeys.STOP_END));
256: }
257:
258: //
259: // ---------- Methods defined in javax.jbi.component.Component ---------
260: //
261:
262: /**
263: * Get the Lifecycle for this entity.
264: * @return the LifeCycle.
265: */
266: public javax.jbi.component.ComponentLifeCycle getLifeCycle() {
267: return this ;
268: }
269:
270: /**
271: * Get the ServiceUnitManager. None is this instance.
272: * @return the ServiceUnitManager.
273: */
274: public javax.jbi.component.ServiceUnitManager getServiceUnitManager() {
275: return null;
276: }
277:
278: /**
279: * Retrieves a DOM representation containing metadata which describes the
280: * service provided by this component, through the given endpoint. The
281: * result can use WSDL 1.1 or WSDL 2.0.
282: *
283: * @param endpoint the service endpoint.
284: * @return the description for the specified service endpoint.
285: */
286: public Document getServiceDescription(ServiceEndpoint endpoint) {
287: return (mProxyBinding.getServiceDescription(endpoint));
288: }
289:
290: /**
291: * This method is called by JBI to check if this component, in the role of
292: * provider of the service indicated by the given exchange, can actually
293: * perform the operation desired.
294: *
295: * @param endpoint the endpoint to be used by the consumer; must be
296: * non-null.
297: * @param exchange the proposed message exchange to be performed; must be
298: * non-null.
299: * @return <code>true</code> if this provider component can perform the
300: * given exchange with the described consumer.
301: */
302: public boolean isExchangeWithConsumerOkay(ServiceEndpoint endpoint,
303: MessageExchange exchange) {
304: //
305: // The ProxyBinding needs to forward this to the remote PB that is hosting
306: // the endpoint.
307: //
308: return (mProxyBinding.isExchangeWithConsumerOkay(endpoint,
309: exchange));
310: }
311:
312: /**
313: * This method is called by JBI to check if this component, in the role of
314: * consumer of the service indicated by the given exchange, can actually
315: * interact with the provider properly. The provider is described by the
316: * given endpoint and the service description supplied by that endpoint.
317: *
318: * @param endpoint the endpoint to be used by the provider; must be
319: * non-null.
320: * @param exchange the proposed message exchange to be performed; must be
321: * non-null.
322: * @return <code>true</code> if this consumer component can interact with
323: * the described provider to perform the given exchange.
324: */
325: public boolean isExchangeWithProviderOkay(ServiceEndpoint endpoint,
326: MessageExchange exchange) {
327: //
328: // The ProxyBinding is never a consumer. So it should be safe to return false.
329: //
330: return true;
331: }
332:
333: /**
334: * Resolve the given endpoint reference. This is called by JBI when it is
335: * attempting to resolve the given EPR on behalf of a component.
336: * <p>
337: * If this component returns a non-null result, it must conform to the
338: * following:
339: * <ul>
340: * <li>This component implements the {@link ServiceEndpoint} returned.
341: * </li>
342: * <li>The result must not be registered or activated with the JBI
343: * implementation.</li>
344: * </ul>
345: *
346: * Dynamically resolved endpoints are distinct from static ones; they must
347: * not be activated (see {@link ComponentContext#activateEndpoint(QName,
348: * String)}), nor registered (see {@link ComponentContext}) by components.
349: * They can only be used to address message exchanges; the JBI
350: * implementation must deliver such exchanges to the component that resolved
351: * the endpoint reference (see {@link
352: * ComponentContext#resolveEndpointReference(DocumentFragment)}).
353: *
354: * @param epr the endpoint reference, in some XML dialect understood by
355: * the appropriate component (usually a binding); must be non-null.
356: * @return the service endpoint for the EPR; <code>null</code> if the
357: * EPR cannot be resolved by this component.
358: */
359: public ServiceEndpoint resolveEndpointReference(DocumentFragment epr) {
360: return (mProxyBinding.resolveEndpointReference(epr));
361: }
362:
363: //
364: // ---------- Methods defined in javax.management.NotificationEmitter ---------
365: //
366:
367: /**
368: * <p>Returns an array indicating, for each notification this
369: * MBean may send, the name of the Java class of the notification
370: * and the notification type.</p>
371: *
372: * <p>It is not illegal for the MBean to send notifications not
373: * described in this array. However, some clients of the MBean
374: * server may depend on the array being complete for their correct
375: * functioning.</p>
376: *
377: * @return the array of possible notifications.
378: */
379: public MBeanNotificationInfo[] getNotificationInfo() {
380: return new MBeanNotificationInfo[] {
381: new MBeanNotificationInfo(
382: new String[] { ESB_MEMBER_JOIN }, String.class
383: .getName(),
384: "This notification is emitted when an ESB instance member joins."),
385: new MBeanNotificationInfo(
386: new String[] { ESB_MEMBER_LEAVE }, String.class
387: .getName(),
388: "This notification is emitted when an ESB instance member leaves.") };
389: }
390:
391: public static final String ESB_MEMBER_JOIN = "ESB-Member-Join";
392: public static final String ESB_MEMBER_LEAVE = "ESB-Member-Leave";
393:
394: //
395: // ---------- Methods locally defined ------------------------------------------
396: //
397: public ComponentContext getComponentContext() {
398: return mContext;
399: }
400:
401: public void postNotification(String type, String value) {
402: Notification note;
403:
404: note = new Notification(type, this , ++mNotificationSequence);
405: note.setUserData(value);
406: getNotificationBroadcaster().sendNotification(note);
407: }
408:
409: /**
410: * Returns a handle to the <code>ProxyBindingStatistics</code> instance.
411: *
412: * @return ProxyBindingStatistics handle to local stats reference.
413: */
414: public ProxyBindingStatistics getProxyBindingStatistics() {
415: return mProxyBindingStatistics;
416: }
417:
418: /**
419: * Displays ProxyBinding state on the server.log.
420: */
421: public void dumpState() {
422: mLog.warning(mProxyBinding.toString());
423: }
424:
425: /**
426: * Register the global statistics MBean.
427: * @throws javax.jbi.JBIException
428: */
429: private void registerGlobalStatsMBean()
430: throws javax.jbi.JBIException {
431: String mBeanType = MBeanNames.CONTROL_TYPE_STATISTICS;
432: String serviceName = ProxyBinding.SUNPROXYBINDINGSERVICE;
433: mProxyBindingStatistics = new ProxyBindingStatistics();
434: Class pbStatsClass = com.sun.jbi.monitoring.ProxyBindingStatisticsMBean.class;
435:
436: mLog.info(Translator.translate(
437: LocalStringKeys.STATS_MBEAN_REGISTERING, new Object[] {
438: mBeanType, "ProxyBinding", "ProxyBinding" }));
439:
440: ObjectName mBeanName = mBeanNamesImpl
441: .getSystemServiceMBeanName(serviceName, mBeanType);
442: MBeanUtils.registerStandardMBean(pbStatsClass,
443: mProxyBindingStatistics, mBeanName, true);
444: }
445:
446: /**
447: * Un-Register the global statistics MBean.
448: * @throws javax.jbi.JBIException
449: */
450: private void unregisterGlobalStatsMBean()
451: throws javax.jbi.JBIException {
452: //MBeanUtils.unregisterStatsMBean(ProxyBinding.SUNPROXYBINDINGSERVICE) ;
453: String mBeanType = MBeanNames.CONTROL_TYPE_STATISTICS;
454: String serviceName = ProxyBinding.SUNPROXYBINDINGSERVICE;
455:
456: ObjectName mBeanName = mBeanNamesImpl
457: .getSystemServiceMBeanName(serviceName, mBeanType);
458:
459: MBeanUtils.unregisterMBean(mBeanName);
460:
461: }
462: }
|