View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module;
35  
36  import info.magnolia.module.model.ModuleDefinition;
37  
38  import java.util.Collections;
39  import java.util.HashMap;
40  import java.util.Map;
41  import java.util.Set;
42  
43  
44  /**
45   * Keeps references to module descriptors and instances.
46   *
47   * @author philipp
48   * @version $Id: ModuleRegistryImpl.java 32667 2010-03-13 00:37:06Z gjoseph $
49   */
50  public class ModuleRegistryImpl implements ModuleRegistry {
51      private final Map<String, ModuleEntry> entries;
52  
53      public ModuleRegistryImpl() {
54          entries = new HashMap<String, ModuleEntry>();
55      }
56  
57      public void registerModuleDefinition(String name, ModuleDefinition moduleDefinition) {
58          getOrCreateModuleEntry(name).moduleDefinition = moduleDefinition;
59      }
60  
61      public void registerModuleInstance(String name, Object moduleInstance) {
62          getOrCreateModuleEntry(name).moduleInstance = moduleInstance;
63      }
64  
65      public void registerModuleVersionHandler(String name, ModuleVersionHandler moduleVersionHandler) {
66          getOrCreateModuleEntry(name).moduleVersionHandler = moduleVersionHandler;
67      }
68  
69      public boolean isModuleRegistered(String name) {
70          return entries.containsKey(name);
71      }
72  
73      public ModuleDefinition getDefinition(String name) {
74          return safeGetModuleEntry(name).moduleDefinition;
75      }
76  
77      public Object getModuleInstance(String name) {
78          return safeGetModuleEntry(name).moduleInstance;
79      }
80  
81      public <T> T getModuleInstance(final Class<T> moduleClass) {
82          T module = null;
83          for (ModuleEntry m : entries.values()) {
84              if (m.moduleInstance != null && moduleClass.isAssignableFrom(m.moduleInstance.getClass())) {
85                  if (module != null) {
86                      throw new IllegalArgumentException("Multiple modules registered with " + moduleClass.toString() + ".");
87                  }
88                  module = (T) m.moduleInstance;
89              }
90          }
91          if (module != null) {
92              return module;
93          }
94          throw new IllegalArgumentException("No module registered with " + moduleClass.toString() + ".");
95      }
96  
97      public ModuleVersionHandler getVersionHandler(String name) {
98          return safeGetModuleEntry(name).moduleVersionHandler;
99      }
100 
101     public Set<String> getModuleNames() {
102         return Collections.unmodifiableSet(entries.keySet());
103     }
104 
105     private ModuleEntry getOrCreateModuleEntry(String name) {
106         synchronized (entries) {
107             ModuleEntry moduleEntry = entries.get(name);
108             if (moduleEntry == null) {
109                 moduleEntry = new ModuleEntry();
110                 entries.put(name, moduleEntry);
111             }
112             return moduleEntry;
113         }
114     }
115 
116     private ModuleEntry safeGetModuleEntry(String name) {
117         synchronized (entries) {
118             final ModuleEntry moduleEntry = entries.get(name);
119             if (moduleEntry == null) {
120                 throw new IllegalArgumentException("No module registered with name \"" + name + "\".");
121             }
122             return moduleEntry;
123         }
124     }
125 
126 
127     private static final class ModuleEntry {
128 
129         private ModuleDefinition moduleDefinition;
130 
131         private Object moduleInstance;
132 
133         private ModuleVersionHandler moduleVersionHandler;
134 
135     }
136 }