KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > client > RemoteServiceMixClient


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.client;
18
19 import javax.jbi.JBIException;
20
21 import org.apache.servicemix.id.IdGenerator;
22 import org.apache.servicemix.jbi.container.ActivationSpec;
23 import org.apache.servicemix.jbi.container.JBIContainer;
24 import org.apache.servicemix.jbi.nmr.flow.jms.JMSFlow;
25
26 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
27
28 /**
29  * Provides remote access to ServiceMix JBI Containers running on the JMS NMR Flow
30  * The RemoteServiceMixClient creates an enbedded JBIContainer and set the
31  * flow to use JMSFlow @see org.apache.servicemix.jbi.nmr.flow.jms.JMSFlow
32  *
33  * @version $Revision: 429658 $
34  */

35 public class RemoteServiceMixClient extends DefaultServiceMixClient {
36
37     private JBIContainer container;
38     private ActivationSpec activationSpec;
39     private String JavaDoc uri;
40     private JMSFlow jmsFlow;
41     private AtomicBoolean initialized = new AtomicBoolean(false);
42     private AtomicBoolean started = new AtomicBoolean(false);
43
44     /**
45      * Create a RemoteServiceMixClient - setting the default
46      * transport for the JMSFlow to be peer://
47      *
48      */

49     public RemoteServiceMixClient(){
50         this("peer://org.apache.servicemix?persistent=false");
51     }
52
53     /**
54      * Create a RemoteServiceMixClient
55      * @param uri
56      *
57      */

58     public RemoteServiceMixClient(String JavaDoc uri){
59         this(uri, new ActivationSpec());
60     }
61
62     /**
63      * Create a RemoteServiceMixClient
64      * @param uri
65      * @param activationSpec
66      */

67     public RemoteServiceMixClient(String JavaDoc uri, ActivationSpec activationSpec){
68         container = new JBIContainer();
69         container.setEmbedded(true);
70         container.setUseMBeanServer(false);
71         container.setName(new IdGenerator().generateSanitizedId());
72         this.uri = uri;
73         this.activationSpec = activationSpec;
74
75     }
76
77     /**
78      * init initializes the embedded JBIContainer
79      *
80      * @throws JBIException
81      */

82     public void init() throws JBIException {
83         if (initialized.compareAndSet(false, true)) {
84             jmsFlow = new JMSFlow();
85             jmsFlow.setJmsURL(uri);
86             container.setFlow(jmsFlow);
87             container.setEmbedded(true);
88             container.setUseMBeanServer(false);
89             container.setCreateMBeanServer(false);
90             container.setMonitorDeploymentDirectory(false);
91             container.setMonitorInstallationDirectory(false);
92             container.init();
93             activationSpec.setComponent(this);
94             container.activateComponent(activationSpec);
95         }
96     }
97
98     /**
99      * Start the item.
100      *
101      * @exception javax.jbi.JBIException
102      * if the item fails to start.
103      */

104     public void start() throws JBIException {
105         start(Long.MAX_VALUE);
106     }
107     
108     public void start(long timeout) throws JBIException {
109         init();
110         if (started.compareAndSet(false, true)) {
111             container.start();
112             if (timeout > 0) {
113                 // Wait for cluster to be connected
114
// This is very ugly but we have no way yet to be notified
115
// of cluster events.
116
long start = System.currentTimeMillis();
117                 while (jmsFlow.numberInNetwork() == 0 &&
118                        System.currentTimeMillis() - start < timeout) {
119                     try {
120                         Thread.sleep(50);
121                     } catch (InterruptedException JavaDoc e) {
122                         throw new JBIException(e);
123                     }
124                 }
125                 if (jmsFlow.numberInNetwork() == 0) {
126                     throw new JBIException("Timeout while connecting to remote JBI container");
127                 }
128             }
129             super.start();
130         }
131     }
132
133     /**
134      * Stop the item. This suspends current messaging activities.
135      *
136      * @exception javax.jbi.JBIException
137      * if the item fails to stop.
138      */

139     public void stop() throws JBIException {
140         super.stop();
141     }
142
143     /**
144      * Shut down the item. The releases resources, preparatory to uninstallation.
145      *
146      * @exception javax.jbi.JBIException
147      * if the item fails to shut down.
148      */

149     public void shutDown() throws JBIException {
150         super.shutDown();
151         container.shutDown();
152     }
153     
154     public String JavaDoc getContainerName() {
155         return container.getName();
156     }
157     
158     public void setContainerName(String JavaDoc name) {
159         container.setName(name);
160     }
161     
162     public void close() throws JBIException {
163         shutDown();
164     }
165
166 }
167
Popular Tags