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: * @(#)LifeCycleImpl.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.internal.security.test.binding1.rt;
030:
031: import java.io.File;
032:
033: import java.util.logging.Logger;
034: import java.util.Vector;
035:
036: import javax.jbi.JBIException;
037: import javax.jbi.component.Component;
038: import javax.jbi.component.ComponentContext;
039: import javax.jbi.component.ComponentLifeCycle;
040: import javax.jbi.component.ServiceUnitManager;
041: import javax.jbi.management.MBeanNames;
042: import javax.jbi.servicedesc.ServiceEndpoint;
043: import javax.management.MBeanServer;
044: import javax.management.ObjectName;
045: import javax.management.StandardMBean;
046:
047: import javax.xml.soap.SOAPMessage;
048: import javax.xml.soap.MessageFactory;
049:
050: import com.sun.jbi.binding.security.SecurityHandler;
051: import com.sun.jbi.binding.security.Endpoint;
052: import com.sun.jbi.management.ManagementMessageBuilder;
053:
054: /**
055: * This is an implementation of a Binding life cycle which starts a
056: * thread. The thread is the main implementation of the binding.
057: *
058: * @author Sun Microsystems, Inc.
059: */
060: public class LifeCycleImpl implements Component, ComponentLifeCycle {
061:
062: /**
063: * Logger instance
064: */
065: private Logger mLog = Logger
066: .getLogger("com.sun.jbi.internal.security.test.binding1");
067: /**
068: * Actual binding implementation
069: */
070: /**
071: * Local copy of the component ID
072: */
073: private String mComponentId;
074: /**
075: * fix it
076: */
077: private ComponentContext mContext;
078: /**
079: * fix it
080: */
081: private ManagementMessageBuilder mMgmtMsgBuilder;
082:
083: /**
084: * fix it
085: */
086: private ServiceUnitMgrImpl mDeployer;
087:
088: /**
089: * The Security Handler.
090: */
091: private SecurityHandler mSecHndlr;
092:
093: /**
094: * Service name.
095: */
096: private String mServiceName = "TestService";
097:
098: /**
099: * TNS.
100: */
101: private String mTns = "http://www.sun.com";
102:
103: /**
104: * Provider Endpoint Prefix
105: */
106: private String mPEPrefix = "ProviderEndpoint";
107:
108: /**
109: * Consumer Endpoint Prefix
110: */
111: private String mCEPrefix = "ConsumerEndpoint";
112:
113: /**
114: * Deployment Folder
115: */
116: private String mDeplDir = "deployment";
117:
118: /**
119: * Message Dir.
120: */
121: private String mMsgDir = "messages";
122:
123: /**
124: * Deployed Endpoint's registry
125: */
126: private Vector mDeplRegistry = new Vector();
127:
128: /**
129: * Get the required life cycle control implementation for this component.
130: * @return the life cycle control implementation for this component.
131: */
132: public ComponentLifeCycle getLifeCycle() {
133: return this ;
134: }
135:
136: /**
137: * Get the Service Unit manager for this component. If this component
138: * does not support deployments, return <code>null</code>.
139: * @return the Service Unit manager for this component, or <code>null</code>
140: * if there is none.
141: */
142: public ServiceUnitManager getServiceUnitManager() {
143: return mDeployer;
144: }
145:
146: /**
147: * Resolve descriptor details for the specified reference, which is for a
148: * service provided by this component.
149: * @param ref the endpoint reference to be resolved.
150: * @return the description for the specified reference.
151: */
152: public org.w3c.dom.Document getServiceDescription(
153: ServiceEndpoint ref) {
154: return null;
155: }
156:
157: /**
158: * Initialize the Binding Component.
159: * @param context the JBI binding environment context created
160: * by the JBI framework
161: * @throws JBIException if an error occurs
162: */
163: public void init(ComponentContext context) throws JBIException {
164:
165: if (context == null) {
166: throw new JBIException("Null argument received for "
167: + "ComponentContext");
168: }
169:
170: this .mLog = Logger
171: .getLogger("com.sun.jbi.internal.security.test.binding1");
172:
173: this .mContext = context;
174: this .mComponentId = context.getComponentName();
175:
176: try {
177: this .mMgmtMsgBuilder = ((com.sun.jbi.component.ComponentContext) mContext)
178: .getManagementMessageFactory()
179: .newBuildManagementMessage();
180: } catch (Exception e) {
181: throw new JBIException("Unable to create Mmgt Msg Builder",
182: e);
183: }
184:
185: createDeployer();
186:
187: mLog.info("Binding " + mComponentId + " initialized");
188:
189: }
190:
191: /**
192: * Get the JMX ObjectName for any additional MBean for this BC. If there
193: * is none, return null.
194: * @return ObjectName the JMX object name of the additional MBean or null
195: * if there is no additional MBean.
196: */
197: public ObjectName getExtensionMBeanName() {
198: return null;
199: }
200:
201: /**
202: * Start the Binding Component.
203: * @throws JBIException if an error occurs
204: */
205: public void start() throws JBIException {
206:
207: mLog.info("Binding " + mComponentId + " started");
208:
209: mSecHndlr = ((com.sun.jbi.component.ComponentContext) mContext)
210: .getSecurityHandler();
211:
212: try {
213:
214: SecurityHelper secHelper = new SecurityHelper();
215:
216: // -- Load the JCE Provider
217: secHelper.loadProvider();
218:
219: registerDeployments();
220:
221: simulateRequestResponse();
222:
223: unregisterDeployments();
224:
225: secHelper.unloadProvider();
226: } catch (Exception ex) {
227: throw new JBIException(ex.getMessage(), ex);
228: }
229:
230: mLog.info("Binding " + mComponentId
231: + " going to sleep .. zz.zzz");
232:
233: try {
234: Thread.sleep(30000);
235: } catch (Exception ex) {
236:
237: }
238:
239: mLog.info("Binding " + mComponentId + " woke up .. G' Morning");
240: }
241:
242: /**
243: * Stop the Binding Component.
244: * @throws JBIException if an error occurs
245: */
246: public void stop() throws JBIException {
247: mLog.info("Binding " + mComponentId + " stopped");
248: }
249:
250: /**
251: * Shut down the Binding Component.
252: * @throws JBIException if an error occurs
253: */
254: public void shutDown() throws JBIException {
255: mLog.info("Binding " + mComponentId + " shut down");
256: }
257:
258: /**
259: * fix it
260: * @return fix it
261: */
262: public Logger getLogger() {
263: return this .mLog;
264: }
265:
266: /**
267: * fix it
268: * @return fix it
269: */
270: public String getId() {
271: return this .mComponentId;
272: }
273:
274: /**
275: * fix it
276: * @return fix it
277: */
278: public ComponentContext getContext() {
279: return this .mContext;
280: }
281:
282: /**
283: * fix it
284: * @return fix it
285: */
286: public ManagementMessageBuilder getMgmtMsgBuilder() {
287: return this .mMgmtMsgBuilder;
288: }
289:
290: /**
291: * fix it
292: * @throws JBIException fix it
293: */
294: public void createDeployer() throws JBIException {
295: try {
296: if (this .mDeployer == null) {
297: this .mDeployer = new ServiceUnitMgrImpl(this );
298: }
299: } catch (Exception e) {
300: throw new JBIException(
301: "Security Test Binding1 Deployer Creation Failed",
302: e);
303: }
304: }
305:
306: /**
307: * This method registers the Endpoint deployments. This test binding bypasses the
308: * deployment contract, to simplify the test.
309: */
310: private void registerDeployments() throws Exception {
311: int i = 1;
312: boolean providerDone = false;
313:
314: do {
315: String deplFile = mContext.getInstallRoot()
316: + File.separator + mDeplDir + File.separator
317: + mPEPrefix + i + ".xml";
318: if (new File(deplFile).exists()) {
319: Endpoint ep = new EndpointImpl(
320: mPEPrefix + i,
321: mServiceName,
322: mTns,
323: mComponentId,
324: javax.jbi.messaging.MessageExchange.Role.PROVIDER,
325: deplFile);
326: mSecHndlr.getDeploymentListener().addDeployment(ep);
327: mDeplRegistry.add(ep);
328: } else {
329: providerDone = true;
330: System.out.println("Deployment File : " + deplFile
331: + " does not exist.");
332: }
333: i++;
334: } while (!providerDone);
335:
336: int j = 1;
337: boolean consumerDone = false;
338:
339: do {
340: String deplFile = mContext.getInstallRoot()
341: + File.separator + mDeplDir + File.separator
342: + mCEPrefix + j + ".xml";
343: if (new File(deplFile).exists()) {
344: Endpoint ep = new EndpointImpl(
345: mCEPrefix + j,
346: mServiceName,
347: mTns,
348: mComponentId,
349: javax.jbi.messaging.MessageExchange.Role.CONSUMER,
350: deplFile);
351: mSecHndlr.getDeploymentListener().addDeployment(ep);
352: mDeplRegistry.add(ep);
353: } else {
354: consumerDone = true;
355: System.out.println("Deployment File : " + deplFile
356: + " does not exist.");
357: }
358: j++;
359: } while (!consumerDone);
360:
361: }
362:
363: /**
364: * This method registers the Endpoint deployments. This test binding bypasses the
365: * deployment contract, to simplify the test.
366: */
367: private void unregisterDeployments() {
368: java.util.Iterator itr = mDeplRegistry.iterator();
369:
370: while (itr.hasNext()) {
371: mSecHndlr.getDeploymentListener().removeDeployment(
372: (Endpoint) itr.next());
373: }
374:
375: }
376:
377: /**
378: * Simulate a request / response
379: */
380: private void simulateRequestResponse() throws Exception {
381: int i = 1;
382: boolean msgTestDone = false;
383:
384: do {
385: // -- Read the sample soap message
386: String msg = mContext.getInstallRoot() + File.separator
387: + mMsgDir + File.separator + "soapmsg0" + i
388: + ".xml";
389: File msgFile = new File(msg);
390:
391: if (msgFile.exists()) {
392: SOAPMessage soapMsg = readSoapMsg(new java.io.FileInputStream(
393: msgFile));
394: Endpoint provider = new EndpointImpl(
395: mPEPrefix + i,
396: mServiceName,
397: mTns,
398: mComponentId,
399: javax.jbi.messaging.MessageExchange.Role.PROVIDER,
400: null);
401: Endpoint consumer = new EndpointImpl(
402: mCEPrefix + i,
403: mServiceName,
404: mTns,
405: mComponentId,
406: javax.jbi.messaging.MessageExchange.Role.CONSUMER,
407: null);
408:
409: // -- Secure Request
410: /** The Provider Implementation does not support the dynamic case
411: javax.security.auth.Subject subject = new javax.security.auth.Subject();
412: subject.getPrivateCredentials().add(
413: new com.sun.jbi.binding.security.PasswordCredential("nikita", "atikin"));
414: */
415: javax.security.auth.Subject subject = null;
416: SOAPMessageContext msgCtx = new SOAPMessageContext(
417: soapMsg);
418: mSecHndlr.getInterceptor().processOutgoingMessage(
419: provider, "default", msgCtx, subject);
420:
421: System.out.println("Secure Message :");
422: ((SOAPMessage) msgCtx.getMessage()).writeTo(System.out);
423:
424: // -- Validate Request
425: subject = null;
426: mSecHndlr.getInterceptor().processIncomingMessage(
427: consumer, "default", msgCtx, subject);
428:
429: // -- Secure Response
430: subject = null;
431: mSecHndlr.getInterceptor().processOutgoingMessage(
432: consumer, "default", msgCtx, subject);
433:
434: System.out.println("Secure Message :");
435: ((SOAPMessage) msgCtx.getMessage()).writeTo(System.out);
436:
437: // -- Validate Response
438: subject = null;
439: mSecHndlr.getInterceptor().processIncomingMessage(
440: provider, "default", msgCtx, subject);
441:
442: ((SOAPMessage) msgCtx.getMessage()).writeTo(System.out);
443: } else {
444: System.out.println("Message File : " + msg
445: + "does not exist.");
446: msgTestDone = true;
447: }
448: i++;
449: } while (!msgTestDone);
450:
451: }
452:
453: /**
454: * Create a SOAP Message from the message stream.
455: */
456: private SOAPMessage readSoapMsg(java.io.InputStream istr)
457: throws Exception {
458: javax.xml.soap.MimeHeaders header = new javax.xml.soap.MimeHeaders();
459: header.addHeader("CONTENT-TYPE", "text/xml");
460: return MessageFactory.newInstance().createMessage(header, istr);
461: }
462:
463: /**
464: * Resolves the specified endpoint reference into a service endpoint. This
465: * is called by the component when it has an endpoint reference that it
466: * needs to resolve into a service endpoint.
467: * @param endpointReference the endpoint reference as an XML fragment.
468: * @return the service endpoint corresponding to the specified endpoint
469: * reference, or <code>null</code> if the reference cannot be resolved.
470: */
471: public ServiceEndpoint resolveEndpointReference(
472: org.w3c.dom.DocumentFragment endpointReference) {
473: return null;
474: }
475:
476: /** This method is called by JBI to check if this component, in the role of
477: * provider of the service indicated by the given exchange, can actually
478: * perform the operation desired.
479: */
480: public boolean isExchangeWithConsumerOkay(
481: javax.jbi.servicedesc.ServiceEndpoint endpoint,
482: javax.jbi.messaging.MessageExchange exchange) {
483: return true;
484: }
485:
486: /** This method is called by JBI to check if this component, in the role of
487: * consumer of the service indicated by the given exchange, can actually
488: * interact with the the provider completely.
489: */
490: public boolean isExchangeWithProviderOkay(
491: javax.jbi.servicedesc.ServiceEndpoint endpoint,
492: javax.jbi.messaging.MessageExchange exchange) {
493: return true;
494: }
495: }
|