Source code: com/flexstor/flexdbserver/services/ServiceManager.java
1 /*
2 * ServiceManager.java
3 *
4 * Copyright $Date: 2003/08/11 02:22:38 $ FLEXSTOR.net Inc.
5 *
6 * This work is licensed for use and distribution under license terms found at
7 * http://www.flexstor.org/license.html
8 *
9 */
10
11 package com.flexstor.flexdbserver.services;
12
13 import java.util.Hashtable;
14
15 import com.flexstor.common.data.ActionData;
16 import com.flexstor.common.data.ActionResult;
17 import com.flexstor.common.services.SrvcNotAvailException;
18 import com.flexstor.common.threadmgr.ThreadCallbackI;
19 import com.flexstor.common.threadmgr.ThreadConsumerI;
20 import com.flexstor.common.threadmgr.ThreadManager;
21
22
23 public abstract class ServiceManager
24 implements ThreadConsumerI
25 {
26 // To get the version number from MKS
27 public final static String IDENTIFIER="$Id: ServiceManager.java,v 1.3 2003/08/11 02:22:38 aleric Exp $";
28
29 private String sMgrName = ""; // Used by thread manager to identify this thread
30 private String sGroupName = ""; // Used by thread manager to identify this thread
31 private ThreadCallbackI caller = null; // Used by thread manager to notify when request is done
32 private ActionResult result = null;
33 private ServiceRunner runner = new ServiceRunner();
34
35 public ServiceManager( ActionData data, ThreadCallbackI caller )
36 {
37 this.caller = caller;
38 sGroupName = ServiceUtil.getThreadGroupName( data );
39 sMgrName = ServiceUtil.getThreadName( data );
40 }
41
42 /**
43 * All the logic for the service manager goes here.
44 */
45 protected abstract ActionResult execute() throws InterruptedException;
46
47 /**
48 * This method need to be implemented for all subclasses to return the proper
49 * result object if the execution of the service manager ends abnormally (due
50 * to a non-catch exception inside the service manager.
51 */
52 protected abstract ActionResult getResultObjectOnAbnormalEnding();
53
54 /**
55 * Start this ServiceManager in a new Thread.
56 */
57 public void startManager()
58 {
59 ThreadManager.requestService( sGroupName, sMgrName, this, caller, null );
60 }
61
62 /**
63 * Check if the Thread running this ServiceManager has been signaled to stop.
64 */
65 protected boolean abortManager()
66 {
67 return ThreadManager.isAborted();
68 }
69
70 /**
71 * This run method will catch any kind of unhandled exception in the sub-service manager
72 * and will return a null value to the caller of this manager.
73 * This is done with the intention of not hanging the caller of the service manager when
74 * sitting in wait mode.
75 */
76 public void processService( Object o )
77 {
78 try
79 {
80 result = this.execute();
81 }
82 catch ( Throwable t )
83 {
84 if ( t instanceof InterruptedException )
85 Thread.currentThread().interrupt(); // reassert
86
87 t.printStackTrace();
88 result = getResultObjectOnAbnormalEnding();
89 }
90 }
91
92 public ActionResult getResult()
93 {
94 return result;
95 }
96
97 /**
98 * Starts the specified service with the specified data object
99 */
100 protected ActionResult doService( String sServiceName, ActionData data )
101 throws SrvcNotAvailException
102 {
103 data.setServiceName(sServiceName);
104 return (ActionResult)runner.performService(data);
105 }
106
107 /**
108 * Starts the specified service with the specified data object.
109 * Before the service is started, we load the properties for this service. This method is mostly
110 * used by the AssetServiceManager, which needs to get some properties from the
111 * roletype_service.config file and load them into the service.
112 */
113 protected ActionResult doService( String sServiceName, Hashtable htProperties, ActionData data )
114 throws SrvcNotAvailException
115 {
116 data.setServiceName(sServiceName);
117 return (ActionResult) runner.performService(data, htProperties);
118 }
119
120 }