PageRenderTime 111ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/xiaozhu/guvnor
Java | 259 lines | 187 code | 37 blank | 35 comment | 15 complexity | 37658de737fa372bc00960aa318e09cb 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 java.math.BigInteger;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. import java.util.Properties;
  21. import javax.crypto.Cipher;
  22. import javax.crypto.spec.SecretKeySpec;
  23. import javax.jcr.LoginException;
  24. import javax.jcr.Repository;
  25. import javax.jcr.RepositoryException;
  26. import javax.jcr.Session;
  27. import org.drools.repository.*;
  28. import org.drools.repository.events.StorageEventManager;
  29. import org.drools.repository.events.CheckinEvent;
  30. import org.jboss.seam.ScopeType;
  31. import org.jboss.seam.annotations.Create;
  32. import org.jboss.seam.annotations.Destroy;
  33. import org.jboss.seam.annotations.Name;
  34. import org.jboss.seam.annotations.Scope;
  35. import org.jboss.seam.annotations.Startup;
  36. import org.slf4j.Logger;
  37. import org.slf4j.LoggerFactory;
  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. 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() {
  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. /** Listen for changes to the repository - for inbox purposes */
  91. public static void registerCheckinListener() {
  92. System.out.println("Registering check-in listener");
  93. StorageEventManager.registerCheckinEvent(new CheckinEvent() {
  94. public void afterCheckin(AssetItem item) {
  95. UserInbox.recordUserEditEvent(item); //to register that she edited...
  96. MailboxService.getInstance().recordItemUpdated(item); //for outgoing...
  97. MailboxService.getInstance().wakeUp();
  98. }
  99. });
  100. System.out.println("Check-in listener up");
  101. }
  102. /** Start up the mailbox, flush out any messages that were left */
  103. private void startMailboxService() {
  104. String username = MAILMAN;
  105. if (properties.containsKey(MAILMAN_USER_PROPERTY)) {
  106. username = properties.get(MAILMAN_USER_PROPERTY);
  107. }
  108. String password = "password";
  109. if (properties.containsKey(MAILMAN_PASSWORD_PROPERTY)) {
  110. password = properties.get(MAILMAN_PASSWORD_PROPERTY);
  111. if ("true".equalsIgnoreCase(properties.get(SECURE_PASSWORDS_PROPERTY))) {
  112. password = decode(password);
  113. }
  114. } else {
  115. log.debug("Could not find property " + MAILMAN_PASSWORD_PROPERTY + " for user " + MAILMAN);
  116. }
  117. mailmanSession = new RulesRepository(newSession(username, password));
  118. MailboxService.getInstance().init(mailmanSession);
  119. MailboxService.getInstance().wakeUp();
  120. }
  121. void create(Session sessionForSetup) {
  122. RulesRepositoryAdministrator admin = new RulesRepositoryAdministrator(sessionForSetup);
  123. if (!admin.isRepositoryInitialized()) {
  124. try {
  125. configurator.setupRepository( sessionForSetup );
  126. } catch (RepositoryException e) {
  127. // TODO Auto-generated catch block
  128. e.printStackTrace();
  129. }
  130. }
  131. //
  132. //Migrate v4 ruleflows to v5
  133. //This section checks if the repository contains drools v4
  134. //ruleflows that need to be migrated to drools v5
  135. //
  136. RulesRepository repo = new RulesRepository(sessionForSetup);
  137. try
  138. {
  139. if ( MigrateRepository.needsRuleflowMigration(repo) )
  140. {
  141. MigrateRepository.migrateRuleflows( repo );
  142. }
  143. }
  144. catch ( RepositoryException e )
  145. {
  146. e.printStackTrace();
  147. throw new RulesRepositoryException(e);
  148. }
  149. }
  150. @Destroy
  151. public void close() {
  152. sessionForSetup.logout();
  153. MailboxService.getInstance().stop();
  154. mailmanSession.logout();
  155. }
  156. public void setHomeDirectory(String home) {
  157. if (home!=null) {
  158. properties.put(JCRRepositoryConfigurator.REPOSITORY_ROOT_DIRECTORY, home);
  159. }
  160. }
  161. public void setRepositoryConfigurator(String clazz) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
  162. if (clazz!=null) {
  163. properties.put(RulesRepositoryConfigurator.CONFIGURATOR_CLASS, clazz);
  164. }
  165. }
  166. /**
  167. * This will create a new Session, based on the current user.
  168. * @return
  169. */
  170. public Session newSession(String userName) {
  171. try {
  172. return configurator.login(userName);
  173. } catch ( LoginException e ) {
  174. throw new RulesRepositoryException( "Unable to login to JCR backend." );
  175. } catch ( RepositoryException e ) {
  176. throw new RulesRepositoryException( e );
  177. }
  178. }
  179. /**
  180. * This will create a new Session, based on the current user.
  181. * @return
  182. */
  183. public Session newSession(String userName, String password) {
  184. try {
  185. return configurator.login(userName, password);
  186. } catch ( LoginException e ) {
  187. throw new RulesRepositoryException("UserName: [ " + userName + "] Unable to login to JCR backend." ,e);
  188. } catch ( RepositoryException e ) {
  189. throw new RulesRepositoryException( e );
  190. }
  191. }
  192. private static String decode(String secret)
  193. {
  194. String decodedPassword = secret;
  195. try {
  196. byte[] kbytes = "jaas is the way".getBytes();
  197. SecretKeySpec key = new SecretKeySpec(kbytes, "Blowfish");
  198. BigInteger n = new BigInteger(secret, 16);
  199. byte[] encoding = n.toByteArray();
  200. //SECURITY-344: fix leading zeros
  201. if (encoding.length % 8 != 0)
  202. {
  203. int length = encoding.length;
  204. int newLength = ((length / 8) + 1) * 8;
  205. int pad = newLength - length; //number of leading zeros
  206. byte[] old = encoding;
  207. encoding = new byte[newLength];
  208. for (int i = old.length - 1; i >= 0; i--)
  209. {
  210. encoding[i + pad] = old[i];
  211. }
  212. }
  213. Cipher cipher = Cipher.getInstance("Blowfish");
  214. cipher.init(Cipher.DECRYPT_MODE, key);
  215. byte[] decode = cipher.doFinal(encoding);
  216. decodedPassword = new String(decode);
  217. } catch (Exception e) {
  218. log.error(e.getMessage(),e);
  219. }
  220. return decodedPassword;
  221. }
  222. }