KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jms > JmsLifeCycle


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jms;
18
19 import java.net.URI JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.jbi.messaging.MessageExchange;
23 import javax.jbi.servicedesc.ServiceEndpoint;
24 import javax.xml.namespace.QName JavaDoc;
25
26 import org.apache.activemq.util.IntrospectionSupport;
27 import org.apache.activemq.util.URISupport;
28 import org.apache.servicemix.common.BaseComponent;
29 import org.apache.servicemix.common.BaseLifeCycle;
30 import org.apache.servicemix.common.Endpoint;
31 import org.apache.servicemix.common.ServiceUnit;
32 import org.apache.servicemix.jbi.security.auth.AuthenticationService;
33 import org.apache.servicemix.jbi.security.auth.impl.JAASAuthenticationService;
34 import org.apache.servicemix.jbi.security.keystore.KeystoreManager;
35
36 public class JmsLifeCycle extends BaseLifeCycle {
37
38     protected JmsConfiguration configuration;
39     
40     public JmsLifeCycle(BaseComponent component) {
41         super(component);
42         configuration = new JmsConfiguration();
43     }
44
45     /* (non-Javadoc)
46      * @see org.apache.servicemix.common.BaseComponentLifeCycle#getExtensionMBean()
47      */

48     protected Object JavaDoc getExtensionMBean() throws Exception JavaDoc {
49         return configuration;
50     }
51
52     /* (non-Javadoc)
53      * @see org.apache.servicemix.common.BaseLifeCycle#doInit()
54      */

55     protected void doInit() throws Exception JavaDoc {
56         super.doInit();
57         configuration.setRootDir(context.getWorkspaceRoot());
58         configuration.load();
59         // Lookup keystoreManager and authenticationService
60
if (configuration.getKeystoreManager() == null) {
61             try {
62                 String JavaDoc name = configuration.getKeystoreManagerName();
63                 Object JavaDoc km = context.getNamingContext().lookup(name);
64                 configuration.setKeystoreManager((KeystoreManager) km);
65             } catch (Exception JavaDoc e) {
66                 // ignore
67
}
68         }
69         if (configuration.getAuthenticationService() == null) {
70             try {
71                 String JavaDoc name = configuration.getAuthenticationServiceName();
72                 Object JavaDoc as = context.getNamingContext().lookup(name);
73                 configuration.setAuthenticationService((AuthenticationService) as);
74             } catch (Exception JavaDoc e) {
75                 configuration.setAuthenticationService(new JAASAuthenticationService());
76             }
77         }
78     }
79     
80     /**
81      * @return Returns the configuration.
82      */

83     public JmsConfiguration getConfiguration() {
84         return configuration;
85     }
86
87     /**
88      * @param configuration The configuration to set.
89      */

90     public void setConfiguration(JmsConfiguration configuration) {
91         this.configuration = configuration;
92     }
93
94     protected QName JavaDoc getEPRServiceName() {
95         return JmsResolvedEndpoint.EPR_SERVICE;
96     }
97     
98     protected Endpoint getResolvedEPR(ServiceEndpoint ep) throws Exception JavaDoc {
99         // We receive an exchange for an EPR that has not been used yet.
100
// Register a provider endpoint and restart processing.
101
JmsEndpoint jmsEp = new JmsEndpoint();
102         jmsEp.setServiceUnit(new ServiceUnit(component));
103         jmsEp.setService(ep.getServiceName());
104         jmsEp.setEndpoint(ep.getEndpointName());
105         jmsEp.setRole(MessageExchange.Role.PROVIDER);
106         URI JavaDoc uri = new URI JavaDoc(ep.getEndpointName());
107         Map JavaDoc map = URISupport.parseQuery(uri.getQuery());
108         if( IntrospectionSupport.setProperties(jmsEp, map, "jms.") ) {
109             uri = URISupport.createRemainingURI(uri, map);
110         }
111         if (uri.getPath() != null) {
112             String JavaDoc path = uri.getSchemeSpecificPart();
113             while (path.startsWith("/")) {
114                 path = path.substring(1);
115             }
116             if (path.startsWith(AbstractJmsProcessor.STYLE_QUEUE + "/")) {
117                 jmsEp.setDestinationStyle(AbstractJmsProcessor.STYLE_QUEUE);
118                 jmsEp.setJmsProviderDestinationName(path.substring(AbstractJmsProcessor.STYLE_QUEUE.length() + 1));
119             } else if (path.startsWith(AbstractJmsProcessor.STYLE_TOPIC + "/")) {
120                 jmsEp.setDestinationStyle(AbstractJmsProcessor.STYLE_TOPIC);
121                 jmsEp.setJmsProviderDestinationName(path.substring(AbstractJmsProcessor.STYLE_TOPIC.length() + 1));
122             }
123         }
124         jmsEp.activateDynamic();
125         // TODO: need to find some usefull syntax for jms
126
// jms://
127
return jmsEp;
128     }
129
130     /**
131      * @return the keystoreManager
132      */

133     public KeystoreManager getKeystoreManager() {
134         return configuration.getKeystoreManager();
135     }
136
137     /**
138      * @param keystoreManager the keystoreManager to set
139      */

140     public void setKeystoreManager(KeystoreManager keystoreManager) {
141         this.configuration.setKeystoreManager(keystoreManager);
142     }
143
144     /**
145      * @return the authenticationService
146      */

147     public AuthenticationService getAuthenticationService() {
148         return configuration.getAuthenticationService();
149     }
150
151     /**
152      * @param authenticationService the authenticationService to set
153      */

154     public void setAuthenticationService(AuthenticationService authenticationService) {
155         this.configuration.setAuthenticationService(authenticationService);
156     }
157
158 }
159
Popular Tags