]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/kernel/Activator.java
6aacfd4931760f6e9c85f8d1724f35cfccf55dca
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / kernel / Activator.java
1 package org.argeo.cms.internal.kernel;
2
3 import java.io.IOException;
4 import java.net.URL;
5 import java.util.Dictionary;
6 import java.util.Hashtable;
7 import java.util.List;
8 import java.util.Locale;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.argeo.cms.CmsException;
13 import org.argeo.node.ArgeoLogger;
14 import org.argeo.node.NodeConstants;
15 import org.argeo.node.NodeDeployment;
16 import org.argeo.node.NodeState;
17 import org.argeo.util.LangUtils;
18 import org.osgi.framework.BundleActivator;
19 import org.osgi.framework.BundleContext;
20 import org.osgi.framework.Constants;
21 import org.osgi.framework.ServiceReference;
22 import org.osgi.service.cm.Configuration;
23 import org.osgi.service.cm.ConfigurationAdmin;
24 import org.osgi.service.cm.ManagedService;
25 import org.osgi.service.condpermadmin.ConditionalPermissionAdmin;
26 import org.osgi.service.log.LogReaderService;
27
28 /**
29 * Activates the {@link Kernel} from the provided {@link BundleContext}. Gives
30 * access to kernel information for the rest of the bundle (and only it)
31 */
32 public class Activator implements BundleActivator {
33 // public final static String SYSTEM_KEY_PROPERTY =
34 // "argeo.security.systemKey";
35 private final Log log = LogFactory.getLog(Activator.class);
36
37 // private final static String systemKey;
38 // static {
39 // System.setProperty(SYSTEM_KEY_PROPERTY, systemKey);
40 // }
41
42 // private static Kernel kernel;
43 private static Activator instance;
44
45 private BundleContext bc;
46 private ConditionalPermissionAdmin permissionAdmin;
47 private LogReaderService logReaderService;
48 private ConfigurationAdmin configurationAdmin;
49
50 private NodeLogger logger;
51 private CmsState nodeState;
52 private CmsDeployment nodeDeployment;
53
54 @Override
55 public void start(BundleContext bundleContext) throws Exception {
56 // try {
57 // kernel = new Kernel();
58 // kernel.init();
59 // } catch (Exception e) {
60 // log.error("Cannot boot kernel", e);
61 // }
62
63 instance = this;
64 this.bc = bundleContext;
65 this.permissionAdmin = getService(ConditionalPermissionAdmin.class);
66 this.logReaderService = getService(LogReaderService.class);
67 this.configurationAdmin = getService(ConfigurationAdmin.class);
68
69 initSecurity();// must be first
70 initArgeoLogger();
71 initNodeState();
72 }
73
74 private void initSecurity() {
75 URL url = getClass().getClassLoader().getResource(KernelConstants.JAAS_CONFIG);
76 System.setProperty("java.security.auth.login.config", url.toExternalForm());
77 }
78
79 private void initArgeoLogger() {
80 logger = new NodeLogger(logReaderService);
81
82 // register
83 bc.registerService(ArgeoLogger.class, logger, null);
84 }
85
86 private void initNodeState() throws IOException {
87 nodeState = new CmsState();
88
89 Object cn;
90 Configuration nodeConf = configurationAdmin.getConfiguration(NodeConstants.NODE_STATE_PID);
91 Dictionary<String, Object> props = nodeConf.getProperties();
92 if (props == null) {
93 if (log.isDebugEnabled())
94 log.debug("Clean node state");
95 Dictionary<String, Object> envProps = new Hashtable<>();
96 // Use the UUID of the first framework run as state UUID
97 cn = bc.getProperty(Constants.FRAMEWORK_UUID);
98 envProps.put(NodeConstants.CN, cn);
99 nodeConf.update(envProps);
100 } else {
101 // Check if state is in line with environment
102 // Dictionary<String, Object> envProps = new Hashtable<>();
103 // for (String key : LangUtils.keys(envProps)) {
104 // Object envValue = envProps.get(key);
105 // Object storedValue = props.get(key);
106 // if (storedValue == null)
107 // throw new CmsException("No state value for env " + key + "=" +
108 // envValue
109 // + ", please clean the OSGi configuration.");
110 // if (!storedValue.equals(envValue))
111 // throw new CmsException("State value for " + key + "=" +
112 // storedValue
113 // + " is different from env value =" + envValue + ", please clean
114 // the OSGi configuration.");
115 // }
116 cn = props.get(NodeConstants.CN);
117 if (cn == null)
118 throw new CmsException("No state UUID available");
119 }
120
121 Dictionary<String, Object> regProps = LangUtils.init(Constants.SERVICE_PID, NodeConstants.NODE_STATE_PID);
122 regProps.put(NodeConstants.CN, cn);
123 bc.registerService(LangUtils.names(NodeState.class, ManagedService.class), nodeState, regProps);
124
125 try {
126 nodeDeployment = new CmsDeployment();
127 bc.registerService(LangUtils.names(NodeDeployment.class), nodeDeployment, null);
128 } catch (RuntimeException e) {
129 e.printStackTrace();
130 throw e;
131 }
132 }
133
134 @Override
135 public void stop(BundleContext bundleContext) throws Exception {
136 nodeState.shutdown();
137
138 instance = null;
139 this.bc = null;
140 this.permissionAdmin = null;
141 this.logReaderService = null;
142 this.configurationAdmin = null;
143
144 // if (kernel != null) {
145 // kernel.destroy();
146 // kernel = null;
147 // }
148
149 }
150
151 private <T> T getService(Class<T> clazz) {
152 ServiceReference<T> sr = bc.getServiceReference(clazz);
153 if (sr == null)
154 throw new CmsException("No service available for " + clazz);
155 return bc.getService(sr);
156 }
157
158 public static NodeState getNodeState() {
159 return instance.nodeState;
160 }
161
162 public String[] getLocales() {
163 // TODO optimize?
164 List<Locale> locales = getNodeState().getLocales();
165 String[] res = new String[locales.size()];
166 for (int i = 0; i < locales.size(); i++)
167 res[i] = locales.get(i).toString();
168 return res;
169 }
170
171 }