/root/projects/repository/source/java/org/alfresco/repo/management/subsystems/SwitchableApplicationContextFactory.java

https://github.com/alfresco-mirror/alfresco-mirror · Java · 201 lines · 106 code · 19 blank · 76 comment · 11 complexity · c57a87568adf9914f037e9222eb5e00b MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2010 Alfresco Software Limited.
  3. *
  4. * This file is part of Alfresco
  5. *
  6. * Alfresco is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Alfresco is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package org.alfresco.repo.management.subsystems;
  20. import java.io.IOException;
  21. import java.util.Collections;
  22. import java.util.Set;
  23. import org.springframework.context.ApplicationContext;
  24. /**
  25. * A configurable proxy for a set of {@link ApplicationContextFactory} beans that allows dynamic selection of one or
  26. * more alternative subsystems via a <code>sourceBeanName</code> property. As with other {@link PropertyBackedBean}s,
  27. * can be stopped, reconfigured, started and tested.
  28. */
  29. public class SwitchableApplicationContextFactory extends AbstractPropertyBackedBean implements
  30. ApplicationContextFactory
  31. {
  32. /** The name of the property holding the bean name of the source {@link ApplicationContextFactory}. */
  33. private static final String SOURCE_BEAN_PROPERTY = "sourceBeanName";
  34. /** The default bean name of the source {@link ApplicationContextFactory}. */
  35. private String sourceBeanName;
  36. /**
  37. * Sets the default bean name of the source {@link ApplicationContextFactory}.
  38. *
  39. * @param sourceBeanName
  40. * the bean name
  41. */
  42. public void setSourceBeanName(String sourceBeanName)
  43. {
  44. this.sourceBeanName = sourceBeanName;
  45. }
  46. /*
  47. * (non-Javadoc)
  48. * @see org.alfresco.repo.management.subsystems.ApplicationContextFactory#getApplicationContext()
  49. */
  50. public ApplicationContext getApplicationContext()
  51. {
  52. this.lock.readLock().lock();
  53. try
  54. {
  55. return ((SwitchableState) getState(true)).getApplicationContext();
  56. }
  57. finally
  58. {
  59. this.lock.readLock().unlock();
  60. }
  61. }
  62. /*
  63. * (non-Javadoc)
  64. * @see org.alfresco.repo.management.subsystems.AbstractPropertyBackedBean#createInitialState()
  65. */
  66. @Override
  67. protected PropertyBackedBeanState createInitialState() throws IOException
  68. {
  69. return new SwitchableState(this.sourceBeanName);
  70. }
  71. /**
  72. * Represents the state of a {@link SwitchableApplicationContextFactory}.
  73. */
  74. protected class SwitchableState implements PropertyBackedBeanState
  75. {
  76. /** The current source application context factory. */
  77. private ApplicationContextFactory sourceApplicationContextFactory;
  78. /** The bean name of the source {@link ApplicationContextFactory}. */
  79. private String sourceBeanName;
  80. /**
  81. * Instantiates a new switchable state.
  82. *
  83. * @param sourceBeanName
  84. * the source bean name
  85. */
  86. protected SwitchableState(String sourceBeanName)
  87. {
  88. this.sourceBeanName = sourceBeanName;
  89. }
  90. /*
  91. * (non-Javadoc)
  92. * @see org.alfresco.enterprise.repo.management.ConfigurableBean#onStart()
  93. */
  94. public void start()
  95. {
  96. if (this.sourceApplicationContextFactory == null)
  97. {
  98. this.sourceApplicationContextFactory = (ApplicationContextFactory) getParent().getBean(
  99. this.sourceBeanName);
  100. this.sourceApplicationContextFactory.start();
  101. }
  102. }
  103. /*
  104. * (non-Javadoc)
  105. * @see org.alfresco.repo.management.SelfDescribingBean#onStop()
  106. */
  107. public void stop()
  108. {
  109. if (this.sourceApplicationContextFactory != null)
  110. {
  111. try
  112. {
  113. this.sourceApplicationContextFactory.stop();
  114. }
  115. catch (Exception e)
  116. {
  117. throw new RuntimeException(e);
  118. }
  119. }
  120. this.sourceApplicationContextFactory = null;
  121. }
  122. /**
  123. * Gets the application context.
  124. *
  125. * @return the application context
  126. */
  127. public ApplicationContext getApplicationContext()
  128. {
  129. if (this.sourceApplicationContextFactory == null)
  130. {
  131. start();
  132. }
  133. return this.sourceApplicationContextFactory.getApplicationContext();
  134. }
  135. /*
  136. * (non-Javadoc)
  137. * @see org.alfresco.repo.management.subsystems.PropertyBackedBean#getProperty(java.lang.String)
  138. */
  139. public String getProperty(String name)
  140. {
  141. if (!name.equals(SwitchableApplicationContextFactory.SOURCE_BEAN_PROPERTY))
  142. {
  143. return null;
  144. }
  145. return this.sourceBeanName;
  146. }
  147. /*
  148. * (non-Javadoc)
  149. * @see org.alfresco.repo.management.subsystems.PropertyBackedBean#getPropertyNames()
  150. */
  151. public Set<String> getPropertyNames()
  152. {
  153. return Collections.singleton(SwitchableApplicationContextFactory.SOURCE_BEAN_PROPERTY);
  154. }
  155. /*
  156. * (non-Javadoc)
  157. * @see org.alfresco.repo.management.subsystems.PropertyBackedBean#setProperty(java.lang.String,
  158. * java.lang.String)
  159. */
  160. public void setProperty(String name, String value)
  161. {
  162. if (!name.equals(SwitchableApplicationContextFactory.SOURCE_BEAN_PROPERTY))
  163. {
  164. throw new IllegalStateException("Illegal attempt to write to property \"" + name + "\"");
  165. }
  166. if (!getParent().containsBean(value))
  167. {
  168. throw new IllegalStateException("\"" + value + "\" is not a valid bean name");
  169. }
  170. if (this.sourceApplicationContextFactory != null)
  171. {
  172. stop();
  173. this.sourceBeanName = value;
  174. start();
  175. }
  176. else
  177. {
  178. this.sourceBeanName = value;
  179. }
  180. }
  181. }
  182. }