PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/guvnor-webapp/src/main/java/org/drools/guvnor/server/repository/RepositoryStartupService.java

https://github.com/matnil/guvnor
Java | 260 lines | 185 code | 34 blank | 41 comment | 17 complexity | 9499e4631d282940390e7cbd9c65eb59 MD5 | raw file
  1. /*
  2. * Copyright 2005 JBoss Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.drools.guvnor.server.repository;
  17. import org.drools.repository.*;
  18. import org.drools.repository.events.CheckinEvent;
  19. import org.drools.repository.events.StorageEventManager;
  20. import org.jboss.seam.ScopeType;
  21. import org.jboss.seam.annotations.Create;
  22. import org.jboss.seam.annotations.Destroy;
  23. import org.jboss.seam.annotations.Name;
  24. import org.jboss.seam.annotations.Scope;
  25. import org.jboss.seam.annotations.Startup;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import javax.crypto.Cipher;
  29. import javax.crypto.spec.SecretKeySpec;
  30. import javax.jcr.LoginException;
  31. import javax.jcr.Repository;
  32. import javax.jcr.RepositoryException;
  33. import javax.jcr.Session;
  34. import java.math.BigInteger;
  35. import java.util.HashMap;
  36. import java.util.Map;
  37. import java.util.Properties;
  38. /**
  39. * This startup class manages the JCR repository, sets it up if necessary.
  40. */
  41. @Scope(ScopeType.APPLICATION)
  42. @Startup
  43. @Name("repositoryConfiguration")
  44. public class RepositoryStartupService {
  45. private static final Logger log = LoggerFactory.getLogger(RepositoryStartupService.class);
  46. private static final String ADMIN = "admin";
  47. private static final String ADMIN_USER_PROPERTY = "org.drools.repository.admin.username";
  48. private static final String ADMIN_PASSWORD_PROPERTY = "org.drools.repository.admin.password";
  49. private static final String MAILMAN = "mailman";
  50. private static final String MAILMAN_USER_PROPERTY = "org.drools.repository.mailman.username";
  51. private static final String MAILMAN_PASSWORD_PROPERTY = "org.drools.repository.mailman.password";
  52. private static final String SECURE_PASSWORDS_PROPERTY = "org.drools.repository.secure.passwords";
  53. private RulesRepositoryConfigurator configurator;
  54. final Map<String, String> properties = new HashMap<String, String>();
  55. Repository repository;
  56. private Session sessionForSetup;
  57. private RulesRepository mailmanSession;
  58. public Repository getRepositoryInstance() {
  59. try {
  60. Properties properties = new Properties();
  61. properties.putAll(this.properties);
  62. configurator = RulesRepositoryConfigurator.getInstance(properties);
  63. repository = configurator.getJCRRepository();
  64. } catch (RepositoryException e) {
  65. log.error(e.getMessage(), e);
  66. }
  67. return repository;
  68. }
  69. @Create
  70. public void create() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
  71. repository = getRepositoryInstance();
  72. String username = ADMIN;
  73. if (properties.containsKey(ADMIN_USER_PROPERTY)) {
  74. username = properties.get(ADMIN_USER_PROPERTY);
  75. }
  76. String password = "password";
  77. if (properties.containsKey(ADMIN_PASSWORD_PROPERTY)) {
  78. password = properties.get(ADMIN_PASSWORD_PROPERTY);
  79. if ("true".equalsIgnoreCase(properties.get(SECURE_PASSWORDS_PROPERTY))) {
  80. password = decode(password);
  81. }
  82. } else {
  83. log.debug("Could not find property " + ADMIN_PASSWORD_PROPERTY + " for user " + ADMIN);
  84. }
  85. sessionForSetup = newSession(username, password);
  86. create(sessionForSetup);
  87. startMailboxService();
  88. registerCheckinListener();
  89. }
  90. /**
  91. * Listen for changes to the repository - for inbox purposes
  92. */
  93. public static void registerCheckinListener() {
  94. System.out.println("Registering check-in listener");
  95. StorageEventManager.registerCheckinEvent(new CheckinEvent() {
  96. public void afterCheckin(AssetItem item) {
  97. UserInbox.recordUserEditEvent(item); //to register that she edited...
  98. MailboxService.getInstance().recordItemUpdated(item); //for outgoing...
  99. MailboxService.getInstance().wakeUp();
  100. }
  101. });
  102. System.out.println("Check-in listener up");
  103. }
  104. public static void removeListeners() {
  105. System.out.println("Removing all listeners...");
  106. StorageEventManager.removeListeners();
  107. System.out.println("Listeners removed...");
  108. }
  109. /**
  110. * Start up the mailbox, flush out any messages that were left
  111. */
  112. private void startMailboxService() {
  113. String username = MAILMAN;
  114. if (properties.containsKey(MAILMAN_USER_PROPERTY)) {
  115. username = properties.get(MAILMAN_USER_PROPERTY);
  116. }
  117. String password = "password";
  118. if (properties.containsKey(MAILMAN_PASSWORD_PROPERTY)) {
  119. password = properties.get(MAILMAN_PASSWORD_PROPERTY);
  120. if ("true".equalsIgnoreCase(properties.get(SECURE_PASSWORDS_PROPERTY))) {
  121. password = decode(password);
  122. }
  123. } else {
  124. log.debug("Could not find property " + MAILMAN_PASSWORD_PROPERTY + " for user " + MAILMAN);
  125. }
  126. mailmanSession = new RulesRepository(newSession(username, password));
  127. MailboxService.getInstance().init(mailmanSession);
  128. MailboxService.getInstance().wakeUp();
  129. }
  130. void create(Session sessionForSetup) {
  131. RulesRepositoryAdministrator admin = new RulesRepositoryAdministrator(sessionForSetup);
  132. if (!admin.isRepositoryInitialized()) {
  133. try {
  134. configurator.setupRepository(sessionForSetup);
  135. } catch (RepositoryException e) {
  136. // TODO Auto-generated catch block
  137. e.printStackTrace();
  138. }
  139. }
  140. //
  141. //Migrate v4 ruleflows to v5
  142. //This section checks if the repository contains drools v4
  143. //ruleflows that need to be migrated to drools v5
  144. //
  145. RulesRepository repo = new RulesRepository(sessionForSetup);
  146. try {
  147. if (MigrateRepository.needsRuleflowMigration(repo)) {
  148. MigrateRepository.migrateRuleflows(repo);
  149. }
  150. } catch (RepositoryException e) {
  151. e.printStackTrace();
  152. throw new RulesRepositoryException(e);
  153. }
  154. }
  155. @Destroy
  156. public void close() {
  157. sessionForSetup.logout();
  158. MailboxService.getInstance().stop();
  159. mailmanSession.logout();
  160. }
  161. public void setHomeDirectory(String home) {
  162. if (home != null) {
  163. properties.put(JCRRepositoryConfigurator.REPOSITORY_ROOT_DIRECTORY, home);
  164. }
  165. }
  166. public void setRepositoryConfigurator(String clazz) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
  167. if (clazz != null) {
  168. properties.put(RulesRepositoryConfigurator.CONFIGURATOR_CLASS, clazz);
  169. }
  170. }
  171. /**
  172. * This will create a new Session, based on the current user.
  173. *
  174. * @return
  175. */
  176. public Session newSession(String userName) {
  177. try {
  178. return configurator.login(userName);
  179. } catch (LoginException e) {
  180. throw new RulesRepositoryException("Unable to login to JCR backend.");
  181. } catch (RepositoryException e) {
  182. throw new RulesRepositoryException(e);
  183. }
  184. }
  185. /**
  186. * This will create a new Session, based on the current user.
  187. *
  188. * @return
  189. */
  190. public Session newSession(String userName, String password) {
  191. try {
  192. return configurator.login(userName, password);
  193. } catch (LoginException e) {
  194. throw new RulesRepositoryException("UserName: [ " + userName + "] Unable to login to JCR backend.", e);
  195. } catch (RepositoryException e) {
  196. throw new RulesRepositoryException(e);
  197. }
  198. }
  199. private static String decode(String secret) {
  200. String decodedPassword = secret;
  201. try {
  202. byte[] kbytes = "jaas is the way".getBytes();
  203. SecretKeySpec key = new SecretKeySpec(kbytes, "Blowfish");
  204. BigInteger n = new BigInteger(secret, 16);
  205. byte[] encoding = n.toByteArray();
  206. //SECURITY-344: fix leading zeros
  207. if (encoding.length % 8 != 0) {
  208. int length = encoding.length;
  209. int newLength = ((length / 8) + 1) * 8;
  210. int pad = newLength - length; //number of leading zeros
  211. byte[] old = encoding;
  212. encoding = new byte[newLength];
  213. for (int i = old.length - 1; i >= 0; i--) {
  214. encoding[i + pad] = old[i];
  215. }
  216. }
  217. Cipher cipher = Cipher.getInstance("Blowfish");
  218. cipher.init(Cipher.DECRYPT_MODE, key);
  219. byte[] decode = cipher.doFinal(encoding);
  220. decodedPassword = new String(decode);
  221. } catch (Exception e) {
  222. log.error(e.getMessage(), e);
  223. }
  224. return decodedPassword;
  225. }
  226. }