KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > shark > container > SharkContainer


1 /*
2  * $Id: SharkContainer.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.shark.container;
26
27 import java.util.Properties JavaDoc;
28
29 import org.ofbiz.base.container.Container;
30 import org.ofbiz.base.container.ContainerException;
31 import org.ofbiz.base.container.ContainerConfig;
32 import org.ofbiz.base.util.Debug;
33 import org.ofbiz.base.util.UtilProperties;
34 import org.ofbiz.base.util.UtilMisc;
35 import org.ofbiz.base.util.GeneralRuntimeException;
36 import org.ofbiz.service.LocalDispatcher;
37 import org.ofbiz.service.GenericDispatcher;
38 import org.ofbiz.service.GenericServiceException;
39 import org.ofbiz.entity.GenericDelegator;
40 import org.ofbiz.entity.GenericValue;
41 import org.ofbiz.entity.GenericEntityException;
42
43 import org.enhydra.shark.Shark;
44 import org.enhydra.shark.corba.SharkCORBAServer;
45 import org.enhydra.shark.api.client.wfservice.AdminInterface;
46 import org.enhydra.shark.api.client.wfservice.RepositoryMgr;
47 import org.enhydra.shark.api.client.wfservice.SharkConnection;
48 import org.enhydra.shark.api.client.wfservice.ExecutionAdministration;
49 import org.enhydra.shark.api.client.wfservice.ConnectFailed;
50 import org.enhydra.shark.api.client.wfservice.NotConnected;
51 import org.enhydra.shark.api.client.wfbase.BaseException;
52 import org.enhydra.shark.api.SharkTransaction;
53 import org.enhydra.shark.api.TransactionException;
54
55 /**
56  * Shark Workflow Engine Container
57  *
58  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
59  * @version $Rev: 5462 $
60  * @since 3.1
61  */

62 public class SharkContainer implements Container, Runnable JavaDoc {
63
64     public static final String JavaDoc module = SharkContainer.class.getName();
65
66     private static GenericDelegator delegator = null;
67     private static LocalDispatcher dispatcher = null;
68     private static GenericValue adminUser = null;
69     private static String JavaDoc adminPass = null;
70     private static Shark shark = null;
71
72     protected String JavaDoc configFile = null;
73     private SharkCORBAServer corbaServer = null;
74     private Thread JavaDoc orbThread = null;
75
76     /**
77      * @see org.ofbiz.base.container.Container#init(java.lang.String[], java.lang.String)
78      */

79     public void init(String JavaDoc[] args, String JavaDoc configFile) {
80         this.configFile = configFile;
81     }
82
83     public boolean start() throws ContainerException {
84         ContainerConfig.Container cfg = ContainerConfig.getContainer("shark-container", configFile);
85         ContainerConfig.Container.Property dispatcherProp = cfg.getProperty("dispatcher-name");
86         ContainerConfig.Container.Property delegatorProp = cfg.getProperty("delegator-name");
87         ContainerConfig.Container.Property adminProp = cfg.getProperty("admin-user");
88         ContainerConfig.Container.Property adminPassProp = cfg.getProperty("admin-pass");
89         ContainerConfig.Container.Property engineName = cfg.getProperty("engine-name");
90         ContainerConfig.Container.Property iiopHost = cfg.getProperty("iiop-host");
91         ContainerConfig.Container.Property iiopPort = cfg.getProperty("iiop-port");
92
93         // check the required delegator-name property
94
if (delegatorProp == null || delegatorProp.value == null || delegatorProp.value.length() == 0) {
95             throw new ContainerException("Invalid delegator-name defined in container configuration");
96         }
97
98         // check the required dispatcher-name property
99
if (dispatcherProp == null || dispatcherProp.value == null || dispatcherProp.value.length() == 0) {
100             throw new ContainerException("Invalid dispatcher-name defined in container configuration");
101         }
102
103         // check the required admin-user property
104
if (adminProp == null || adminProp.value == null || adminProp.value.length() == 0) {
105             throw new ContainerException("Invalid admin-user defined in container configuration");
106         }
107
108         if (adminPassProp == null || adminPassProp.value == null || adminPassProp.value.length() == 0) {
109             throw new ContainerException("Invalid admin-pass defined in container configuration");
110         }
111
112         if (engineName == null || engineName.value == null || engineName.value.length() == 0) {
113             throw new ContainerException("Invalid engine-name defined in container configuration");
114         }
115
116         // get the delegator and dispatcher objects
117
SharkContainer.delegator = GenericDelegator.getGenericDelegator(delegatorProp.value);
118         try {
119             SharkContainer.dispatcher = GenericDispatcher.getLocalDispatcher(dispatcherProp.value, SharkContainer.delegator);
120         } catch (GenericServiceException e) {
121             throw new ContainerException(e);
122         }
123
124         // get the admin user
125
try {
126             SharkContainer.adminUser = delegator.findByPrimaryKey("UserLogin", UtilMisc.toMap("userLoginId", adminProp.value));
127         } catch (GenericEntityException e) {
128             throw new ContainerException(e);
129         }
130
131         // make sure the admin user exists
132
if (SharkContainer.adminUser == null) {
133             Debug.logWarning("Invalid admin-user; UserLogin not found not starting Shark!", module);
134             return false;
135         }
136         
137         SharkContainer.adminPass = adminPassProp.value;
138
139         // set the Shark configuration
140
Properties JavaDoc props = UtilProperties.getProperties("shark.properties");
141         Shark.configure(props);
142
143         SharkContainer.shark = Shark.getInstance();
144         Debug.logInfo("Started Shark workflow service", module);
145
146         // create the CORBA server and bind to iiop
147
if (iiopHost != null && iiopHost.value != null && iiopHost.value.length() > 0) {
148             if (iiopPort != null && iiopPort.value != null && iiopPort.value.length() > 0) {
149                 try {
150                     corbaServer = new SharkCORBAServer(engineName.value, iiopHost.value, iiopPort.value, shark);
151                     orbThread = new Thread JavaDoc(this);
152                     orbThread.setDaemon(false);
153                     orbThread.setName(this.getClass().getName());
154                     orbThread.start();
155                     Debug.logInfo("Started Shark CORBA service", module);
156                 } catch (IllegalArgumentException JavaDoc e) {
157                     throw new ContainerException(e);
158                 } catch (GeneralRuntimeException e) {
159                     throw new ContainerException(e);
160                 }
161             }
162         }
163        
164         // re-eval current assignments
165
ExecutionAdministration exAdmin = SharkContainer.getAdminInterface().getExecutionAdministration();
166         try {
167             exAdmin.connect(adminUser.getString("userLoginId"), SharkContainer.adminPass, null, null);
168             // this won't work with encrypted passwords: exAdmin.connect(adminUser.getString("userLoginId"), adminUser.getString("currentPassword"), null, null);
169
exAdmin.reevaluateAssignments();
170             exAdmin.disconnect();
171         } catch (ConnectFailed e) {
172             String JavaDoc errMsg = "Shark Connection error (if it is a password wrong error, check the admin-pass property in the container config file, probably ofbiz-containers.xml): " + e.toString();
173             throw new ContainerException(errMsg, e);
174         } catch (NotConnected e) {
175             throw new ContainerException(e);
176         } catch (BaseException e) {
177             throw new ContainerException(e);
178         }
179
180         return true;
181     }
182
183     public void run() {
184         try {
185             corbaServer.startCORBAServer();
186         } catch (BaseException e) {
187             throw new GeneralRuntimeException(e);
188         }
189     }
190
191     public void stop() throws ContainerException {
192         // shut down the dispatcher
193
if (dispatcher != null) {
194             dispatcher.deregister();
195         }
196
197         // shutdown the corba server
198
if (corbaServer != null) {
199             corbaServer.shutdownORB();
200         }
201         Debug.logInfo("stop Shark", module);
202     }
203
204     // static helper methods
205
public static GenericDelegator getDelegator() {
206         return SharkContainer.delegator;
207     }
208
209     public static LocalDispatcher getDispatcher() {
210         return SharkContainer.dispatcher;
211     }
212
213     public static GenericValue getAdminUser() {
214         return SharkContainer.adminUser;
215     }
216
217     public static AdminInterface getAdminInterface() {
218         return shark.getAdminInterface();
219     }
220
221     public static RepositoryMgr getRepositoryMgr() {
222         return shark.getRepositoryManager();
223     }
224
225     public static SharkConnection getSharkConntection() {
226         return shark.getSharkConnection();
227     }
228
229     public static SharkTransaction getTransaction() throws TransactionException {
230         return shark.createTransaction();
231     }
232 }
Popular Tags