001: package org.enhydra.shark.client.utilities;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.InputStream;
006: import java.lang.reflect.Constructor;
007: import java.lang.reflect.Method;
008: import java.net.URL;
009: import java.net.URLConnection;
010: import java.util.Iterator;
011: import java.util.Map;
012: import java.util.Properties;
013:
014: import javax.naming.Context;
015: import javax.naming.InitialContext;
016: import javax.rmi.PortableRemoteObject;
017: import javax.transaction.UserTransaction;
018:
019: import org.enhydra.dods.jts.LocalContextFactory;
020: import org.enhydra.shark.Shark;
021: import org.enhydra.shark.api.admin.ApplicationMappingManagerAdmin;
022: import org.enhydra.shark.api.admin.ParticipantMappingManagerAdmin;
023: import org.enhydra.shark.api.admin.UserGroupManagerAdmin;
024: import org.enhydra.shark.api.client.wfmc.wapi.WAPI;
025: import org.enhydra.shark.api.client.wfmc.wapi.WMConnectInfo;
026: import org.enhydra.shark.api.client.wfmc.wapi.WMSessionHandle;
027: import org.enhydra.shark.api.client.wfservice.SharkConnection;
028: import org.enhydra.shark.api.client.wfservice.SharkInterface;
029: import org.enhydra.shark.api.common.SharkConstants;
030:
031: public class SharkInterfaceWrapper {
032:
033: public static final String CLIENT_TYPE_POJO = "POJO";
034:
035: public static final String CLIENT_TYPE_EJB = "EJB";
036:
037: public static final String CLIENT_TYPE_EJB_GERONIMO = "EJB_GERONIMO";
038:
039: public static final String CLIENT_TYPE_WS = "WS";
040:
041: protected static Properties properties = new Properties();
042:
043: protected static Properties env = new Properties();
044:
045: protected static String xaUTLookupName;
046:
047: protected static String defaultUser = "admin";
048:
049: protected static int defUTTimeout = -1;
050:
051: protected static SharkInterface shark;
052:
053: protected static String tmpShkCfgFileFolder;
054:
055: protected static boolean initTransMgr;
056:
057: // Transaction manager will be initialized only if client type is POJO
058: public static void setProperties(String confFilePath,
059: boolean initTransMgr) throws Exception {
060: Properties props = new Properties();
061: FileInputStream fis = null;
062: File cfgFile = new File(confFilePath);
063: if (!cfgFile.isAbsolute()) {
064: cfgFile = cfgFile.getAbsoluteFile();
065: }
066: if (cfgFile.exists()) {
067: fis = new FileInputStream(confFilePath);
068: props.load(fis);
069: fis.close();
070: tmpShkCfgFileFolder = cfgFile.getParent();
071: }
072: setProperties(props, initTransMgr);
073: }
074:
075: // Transaction manager will be initialized only if client type is POJO
076: public static void setProperties(Properties properties,
077: boolean initTransMgr) throws Exception {
078: SharkInterfaceWrapper.properties = properties;
079: SharkInterfaceWrapper.initTransMgr = initTransMgr;
080: String clientType = getClientType();
081:
082: try {
083: defUTTimeout = Integer.parseInt(properties.getProperty(
084: "UserTransaction.Timeout", "300"));
085: } catch (Exception ex) {
086: defUTTimeout = 300;
087: }
088: if (clientType.equals(CLIENT_TYPE_POJO)) {
089: xaUTLookupName = properties.getProperty(
090: "XaUserTransactionLookupName",
091: "java:comp/UserTransaction");
092: if (initTransMgr) {
093: LocalContextFactory.setup("sharkdb");
094: }
095: } else if (clientType.equals(CLIENT_TYPE_EJB)) {
096: xaUTLookupName = properties.getProperty(
097: "XaUserTransactionLookupName", "UserTransaction");
098: env
099: .setProperty(
100: javax.naming.Context.INITIAL_CONTEXT_FACTORY,
101: properties
102: .getProperty(
103: javax.naming.Context.INITIAL_CONTEXT_FACTORY,
104: "org.jboss.naming.NamingContextFactory"));
105: env.setProperty(Context.URL_PKG_PREFIXES, properties
106: .getProperty(Context.URL_PKG_PREFIXES,
107: "org.jboss.naming:org.jnp.interface"));
108: env.setProperty(javax.naming.Context.PROVIDER_URL,
109: properties.getProperty(
110: javax.naming.Context.PROVIDER_URL,
111: "jnp://localhost:1099"));
112: } else if (clientType.equals(CLIENT_TYPE_EJB_GERONIMO)) {
113: xaUTLookupName = properties.getProperty(
114: "XaUserTransactionLookupName", "UserTransaction");
115: env
116: .setProperty(
117: javax.naming.Context.INITIAL_CONTEXT_FACTORY,
118: properties
119: .getProperty(
120: javax.naming.Context.INITIAL_CONTEXT_FACTORY,
121: "org.openejb.client.RemoteInitialContextFactory"));
122: env.setProperty(javax.naming.Context.PROVIDER_URL,
123: properties.getProperty(
124: javax.naming.Context.PROVIDER_URL,
125: "localhost:4201"));
126: env.setProperty(Context.URL_PKG_PREFIXES, properties
127: .getProperty(Context.URL_PKG_PREFIXES,
128: "org.apache.geronimo.naming"));
129:
130: env.setProperty(javax.naming.Context.SECURITY_PRINCIPAL,
131: properties.getProperty(
132: javax.naming.Context.SECURITY_PRINCIPAL,
133: "system"));
134: env.setProperty(javax.naming.Context.SECURITY_CREDENTIALS,
135: properties.getProperty(
136: javax.naming.Context.SECURITY_CREDENTIALS,
137: "manager"));
138: }
139: System.out.println("XA UserTransaction lookup name is "
140: + xaUTLookupName);
141: defaultUser = properties.getProperty(
142: "DEFAULT_ADMINISTRATOR_USERNAME", "admin");
143: }
144:
145: public static Properties getProperties() {
146: return properties;
147: }
148:
149: public static void setProperty(String propName, String propValue)
150: throws Exception {
151: properties.setProperty(propName, propValue);
152: }
153:
154: public static String getProperty(String propName) {
155: return properties.getProperty(propName);
156: }
157:
158: public static String getUserTransactionLookupName() {
159: return xaUTLookupName;
160: }
161:
162: public static Properties getUserTransactionLookupProperties() {
163: Properties copy = new Properties();
164: Iterator it = env.entrySet().iterator();
165: while (it.hasNext()) {
166: Map.Entry me = (Map.Entry) it.next();
167: copy.setProperty((String) me.getKey(), (String) me
168: .getValue());
169: }
170: return copy;
171: }
172:
173: public static String getDefaultUser() {
174: return defaultUser;
175: }
176:
177: public static SharkInterface getShark() throws Exception {
178: if (shark == null) {
179: String clientType = getClientType();
180: if (clientType.equals(CLIENT_TYPE_POJO)) {
181: if (Shark.isConfigured()) {
182: shark = Shark.getInstance();
183: } else {
184: UserTransaction ut = null;
185: try {
186: ut = getUserTransaction();
187: ut.begin();
188: String shkCfgFilePath = properties
189: .getProperty("SharkConfFileLocation");
190: String shkCfgResourceLocation = properties
191: .getProperty("SharkConfResourceLocation");
192: File shkCfgFile = null;
193: if (shkCfgFilePath != null) {
194: shkCfgFile = new File(shkCfgFilePath);
195: if (!shkCfgFile.isAbsolute()) {
196: if (tmpShkCfgFileFolder != null) {
197: shkCfgFilePath = tmpShkCfgFileFolder
198: + File.separator
199: + shkCfgFile;
200: shkCfgFile = new File(
201: shkCfgFilePath);
202: } else {
203: shkCfgFile = shkCfgFile
204: .getAbsoluteFile();
205: shkCfgFilePath = shkCfgFile
206: .getPath();
207: }
208: }
209: if (shkCfgFile != null
210: && shkCfgFile.exists()) {
211: Shark.configure(shkCfgFilePath);
212: }
213: } else if (shkCfgResourceLocation != null) {
214: URL u = SharkInterfaceWrapper.class
215: .getClassLoader().getResource(
216: shkCfgResourceLocation);
217: URLConnection urlConnection = u
218: .openConnection();
219: InputStream is = urlConnection
220: .getInputStream();
221: Properties props = new Properties();
222: props.load(is);
223: is.close();
224: Shark.configure(props);
225: } else {
226: Shark.configure(properties);
227: }
228: shark = Shark.getInstance();
229: ut.commit();
230: } catch (Throwable e) {
231: e.printStackTrace();
232: throw new Error(e);
233: }
234: System.out
235: .println("Shark Engine Reference Obtained through POJO interface!");
236: }
237: } else if (clientType.startsWith(CLIENT_TYPE_EJB)) {
238: try {
239: String sharkLookupName = "org/enhydra/shark/ejb/SharkEJB";
240: Context initialContext = new InitialContext(env);
241: Object narrowFrom = initialContext
242: .lookup(sharkLookupName);
243: Object obj = PortableRemoteObject
244: .narrow(
245: narrowFrom,
246: Class
247: .forName("org.enhydra.shark.ejb.SharkEJBHome"));
248: Method m = obj.getClass().getMethod("create", null);
249: shark = (SharkInterface) m.invoke(obj, null);
250: } catch (Throwable e) {
251: e.printStackTrace();
252: throw new Error(e);
253: }
254: } else if (clientType.equals(CLIENT_TYPE_WS)) {
255: // vr shark = new SharkWSFactory(properties.getProperty("SharkWSURLPrefix",
256: // "http://localhost:8080/sharkWebServices"));
257:
258: // vr The reflection is used because this class does nor exist if
259: // build process is made withouth Web Services included
260: Object[] args = new Object[] { properties.getProperty(
261: "SharkWSURLPrefix",
262: "http://localhost:8080/sharkWebServices") };
263: Class shFactory = Class
264: .forName("org.enhydra.shark.client.utilities.SharkWSFactory");
265: Constructor cnstrctr = shFactory
266: .getConstructor(new Class[] { java.lang.String.class });
267: shark = (SharkInterface) cnstrctr.newInstance(args);
268: }
269: createDefaultAdminGroupAndUser();
270: }
271: return shark;
272: }
273:
274: public static ParticipantMappingManagerAdmin getParticipantMappingsAdmin() {
275: ParticipantMappingManagerAdmin participantMappingAdmin = null;
276: try {
277: Object pma = getShark().getPlugIn(
278: SharkConstants.PLUGIN_PARTICIPANT_MAPPING);
279: if (pma instanceof ParticipantMappingManagerAdmin) {
280: participantMappingAdmin = (ParticipantMappingManagerAdmin) pma;
281: }
282: } catch (Exception e) {
283: }
284: return participantMappingAdmin;
285: }
286:
287: public static ApplicationMappingManagerAdmin getApplicationMappingsAdmin() {
288: ApplicationMappingManagerAdmin applicationMappingAdmin = null;
289:
290: try {
291: Object ama = getShark().getPlugIn(
292: SharkConstants.PLUGIN_APPLICATION_MAPPING);
293: if (ama instanceof ApplicationMappingManagerAdmin) {
294: applicationMappingAdmin = (ApplicationMappingManagerAdmin) ama;
295: }
296: } catch (Exception e) {
297: }
298:
299: return applicationMappingAdmin;
300: }
301:
302: public static UserGroupManagerAdmin getUserGroupAdmin() {
303: UserGroupManagerAdmin userGroupAdmin = null;
304: try {
305: Object ama = getShark().getPlugIn(
306: SharkConstants.PLUGIN_USER_GROUP);
307: if (ama instanceof UserGroupManagerAdmin) {
308: userGroupAdmin = (UserGroupManagerAdmin) ama;
309: }
310: } catch (Exception e) {
311: }
312:
313: return userGroupAdmin;
314: }
315:
316: public static WMSessionHandle makeDefaultWAPIConnection(WAPI wapi,
317: Object vendorSpecificData) throws Exception {
318: return makeWAPIConnection(wapi, defaultUser, vendorSpecificData);
319: }
320:
321: public static WMSessionHandle makeWAPIConnection(WAPI wapi,
322: String username, Object vendorSpecificData)
323: throws Exception {
324: WMConnectInfo wmci = new WMConnectInfo(username, "", "", "");
325: WMSessionHandle shandle = wapi.connect(wmci);
326: if (vendorSpecificData != null) {
327: shandle.setVendorData(vendorSpecificData);
328: }
329: return shandle;
330: }
331:
332: public static SharkConnection getDefaultSharkConnection(
333: Object vendorSpecificData) throws Exception {
334: return getSharkConnection(defaultUser, vendorSpecificData);
335: }
336:
337: public static SharkConnection getSharkConnection(String username,
338: Object vendorSpecificData) throws Exception {
339: SharkConnection sc = getShark().getSharkConnection();
340: WMConnectInfo wmci = new WMConnectInfo(username, "", "", "");
341: sc.connect(wmci);
342: if (vendorSpecificData != null) {
343: sc.getSessionHandle().setVendorData(vendorSpecificData);
344: }
345: return sc;
346: }
347:
348: public static WMSessionHandle getDefaultSessionHandle(
349: Object vendorSpecificData) throws Exception {
350: return getSessionHandle(defaultUser, vendorSpecificData);
351: }
352:
353: public static WMSessionHandle getSessionHandle(String username,
354: Object vendorSpecificData) throws Exception {
355: SharkConnection sc = null;
356: WMSessionHandle shandle = null;
357: try {
358: sc = getShark().getSharkConnection();
359: } catch (Exception ex) {
360: }
361: WMConnectInfo wmci = new WMConnectInfo(username, "", "", "");
362: if (sc != null) {
363: sc.connect(wmci);
364: shandle = sc.getSessionHandle();
365: } else {
366: WAPI wapi = getShark().getWAPIConnection();
367: shandle = wapi.connect(wmci);
368: }
369: if (vendorSpecificData != null) {
370: shandle.setVendorData(vendorSpecificData);
371: }
372:
373: return shandle;
374: }
375:
376: protected static void createDefaultAdminGroupAndUser() {
377: // create default user
378: UserTransaction ut = null;
379: try {
380: UserGroupManagerAdmin uga = getUserGroupAdmin();
381: if (uga != null) {
382: String adminGroup = properties.getProperty(
383: "DEFAULT_ADMINISTRATOR_GROUP_NAME", "admin");
384: String adminGroupDescripton = properties.getProperty(
385: "DEFAULT_ADMINISTRATOR_GROUP_DESCRIPTION", "");
386: String adminUsername = properties.getProperty(
387: "DEFAULT_ADMINISTRATOR_USERNAME", "admin");
388: String adminPwd = properties.getProperty(
389: "DEFAULT_ADMINISTRATOR_PASSWORD", "enhydra");
390: String adminFirstName = properties.getProperty(
391: "DEFAULT_ADMINISTRATOR_FIRST_NAME",
392: "Administrator");
393: String adminLastName = properties.getProperty(
394: "DEFAULT_ADMINISTRATOR_LAST_NAME", "Together");
395: String adminEmail = properties.getProperty(
396: "DEFAULT_ADMINISTRATOR_EMAIL",
397: "admin@together.at");
398: ut = getUserTransaction();
399: ut.begin();
400: WMSessionHandle shandle = SharkInterfaceWrapper
401: .getSessionHandle(adminUsername, null);
402: if (!uga.doesGroupExist(shandle, adminGroup)) {
403: uga.createGroup(adminGroup, adminGroupDescripton);
404: }
405: ut.commit();
406: ut.begin();
407: if (!uga.doesUserExist(shandle, adminUsername)) {
408: uga.createUser(adminGroup, adminUsername, adminPwd,
409: adminFirstName, adminLastName, adminEmail);
410: } else if (!uga.doesUserBelongToGroup(shandle,
411: adminGroup, adminUsername)) {
412: uga.addUserToGroup(adminGroup, adminUsername);
413:
414: }
415: ut.commit();
416: }
417: } catch (Exception ex) {
418: ex.printStackTrace();
419: try {
420: ut.rollback();
421: } catch (Exception _) {
422: }
423: }
424:
425: }
426:
427: public static void killShark() {
428: shark = null;
429: }
430:
431: public static void shutdown() {
432: if (getClientType().equals(CLIENT_TYPE_POJO) && initTransMgr) {
433: try {
434: LocalContextFactory.shutdown();
435: } catch (Exception ex) {
436: System.err
437: .println("Failed to shutdown LocalContextFactory, err msg="
438: + ex.getMessage());
439: }
440: }
441: }
442:
443: public static UserTransaction getUserTransaction() throws Exception {
444: UserTransaction ut = null;
445: String clientType = getClientType();
446: if (clientType.equals(CLIENT_TYPE_EJB_GERONIMO)
447: || clientType.equals(CLIENT_TYPE_WS)) {
448: ut = new DummyUserTransaction();
449: } else {
450: Context initialContext = new InitialContext(env);
451: ut = (UserTransaction) initialContext
452: .lookup(xaUTLookupName);
453: }
454: ut.setTransactionTimeout(defUTTimeout);
455: return ut;
456: }
457:
458: public static String getClientType() {
459: return properties.getProperty("ClientType", CLIENT_TYPE_POJO);
460: }
461:
462: }
|