1 package org.smartcomps.twister.common.lifecycle;
2
3 import org.smartcomps.twister.common.configuration.XMLConfigurationReader;
4 import org.smartcomps.twister.common.configuration.ConfigurationException;
5 import org.smartcomps.twister.common.configuration.CommonConfiguration;
6 import org.smartcomps.twister.common.transaction.TransactionManager;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import java.util.*;
12
13 public class LifecycleManager {
14
15 private static Log log = LogFactory.getLog(TransactionManager.class);
16 private static Integer NEW = new Integer(0);
17 private static Integer STARTED = new Integer(1);
18 private static Integer STOPPED = new Integer(2);
19 private static Integer DESTROYED = new Integer(3);
20
21 private static LifecycleManager instance;
22
23 private static Map resources;
24
25 public static LifecycleManager getLifecycleManager() throws LifecycleException {
26 if (instance == null) {
27 instance = new LifecycleManager();
28 try {
29 XMLConfigurationReader.loadConfiguration();
30 } catch (ConfigurationException e) {
31 throw new LifecycleException("Could not initialize Twister's configuration.", e);
32 }
33
34 try {
35 resources = new HashMap();
36 List resourceNames = CommonConfiguration.getLifecycleResources();
37 for (int m = 0; m < resourceNames.size(); m++) {
38 String resName = (String) resourceNames.get(m);
39 LifecycleAwareResource lfAwareResource = (LifecycleAwareResource)Class.forName(resName).newInstance();
40 resources.put(lfAwareResource, NEW);
41 }
42 } catch (InstantiationException e) {
43 log.fatal("Could not instantiate a lifecycle resource.", e);
44 System.exit(1);
45 } catch (IllegalAccessException e) {
46 log.fatal("Could not access a lifecycle resource.", e);
47 System.exit(1);
48 } catch (ClassCastException e) {
49 log.fatal("One of the resources declared in twister's configuration is not a lifecycle resource.", e);
50 System.exit(1);
51 } catch (ClassNotFoundException e) {
52 log.fatal("Could not find a class declared as a lifecycle resource.", e);
53 System.exit(1);
54 }
55 }
56 return instance;
57 }
58
59 public void createResources() throws LifecycleException {
60 for (Iterator iterator = resources.keySet().iterator(); iterator.hasNext();) {
61 LifecycleAwareResource resource = (LifecycleAwareResource) iterator.next();
62 if (resources.get(resource).equals(NEW)) {
63 resource.create();
64 resources.put(resource, STOPPED);
65 } else {
66 throw new LifecycleException("Could not create a resource, it has a wrong status : " +
67 resource.getClass().getName());
68 }
69 }
70 }
71
72 public void startResources() throws LifecycleException {
73 for (Iterator iterator = resources.keySet().iterator(); iterator.hasNext();) {
74 LifecycleAwareResource resource = (LifecycleAwareResource) iterator.next();
75 if (resources.get(resource).equals(STOPPED)) {
76 resource.start();
77 resources.put(resource, STARTED);
78 } else {
79 throw new LifecycleException("Could not create a resource, it has a wrong status : " +
80 resource.getClass().getName());
81 }
82 }
83 }
84
85 public void stopResources() throws LifecycleException {
86 for (Iterator iterator = resources.keySet().iterator(); iterator.hasNext();) {
87 LifecycleAwareResource resource = (LifecycleAwareResource) iterator.next();
88 if (resources.get(resource).equals(STARTED)) {
89 resource.stop();
90 resources.put(resource, STOPPED);
91 } else {
92 throw new LifecycleException("Could not create a resource, it has a wrong status : " +
93 resource.getClass().getName());
94 }
95 }
96 }
97
98 public void destroyResources() throws LifecycleException {
99 for (Iterator iterator = resources.keySet().iterator(); iterator.hasNext();) {
100 LifecycleAwareResource resource = (LifecycleAwareResource) iterator.next();
101 if (resources.get(resource).equals(STOPPED)) {
102 resource.destroy();
103 resources.put(resource, DESTROYED);
104 } else {
105 throw new LifecycleException("Could not create a resource, it has a wrong status : " +
106 resource.getClass().getName());
107 }
108 }
109 resources = null;
110 instance = null;
111 }
112
113 }
This page was automatically generated by Maven