View Javadoc
1   /*
2    * #%L
3    * EUGene :: EUGene
4    * %%
5    * Copyright (C) 2004 - 2010 CodeLutin
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  
23  package org.nuiton.eugene.models.object.xml;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.nuiton.eugene.GeneratorUtil;
28  
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  /**
33   * ExternalCacheExtension
34   *
35   * Created: 2 nov. 2009
36   *
37   * @author Florian Desbois - desbois@codelutin.com
38   */
39  public class ExternalCacheExtension {
40  
41      private static final Log log = LogFactory.getLog(ExternalCacheExtension.class);
42  
43      public static final String OBJECTMODEL_EXTENSION = "externalClassifiers";
44  
45      protected Map<String, ObjectModelClassifierImpl> cache;
46  
47      public ExternalCacheExtension() {
48          cache = new HashMap<>();
49      }
50  
51      @SuppressWarnings("unchecked")
52      public <C extends ObjectModelClassifierImpl> C getCache(
53              ObjectModelImplRef reference, Class<C> classifierClass)
54              throws RuntimeException {
55          ObjectModelClassifierImpl classifier = cache.get(reference.getName());
56          C result;
57          if (classifier != null &&
58              !classifierClass.isAssignableFrom(classifier.getClass())) {
59              throw new ClassCastException(
60                      "Invalid cast for " + classifierClass.getName());
61          }
62          if (classifier == null) {
63              try {
64                  result = classifierClass.newInstance();
65                  addClassifierToCache(reference, result);
66                  if (log.isDebugEnabled()) {
67                      log.debug("Add '" + reference.getName() +
68                                "' to external cache");
69                  }
70              } catch (Exception eee) {
71                  // IllegalAccessException and InstantiationException
72                  throw new RuntimeException(
73                          "Unable to add new '" + classifierClass.getName() +
74                          "' to cache for '" + reference.getName() + "'", eee);
75              }
76          } else {
77              if (log.isDebugEnabled()) {
78                  log.debug("Get '" + reference.getName() +
79                            "' from external cache");
80              }
81              result = (C) classifier;
82          }
83          return result;
84      }
85  
86      protected void addClassifierToCache(ObjectModelImplRef reference,
87                                          ObjectModelClassifierImpl classifier) {
88          String fqn = reference.getName();
89          String packageName = GeneratorUtil.getParentPackageName(fqn);
90          String name = GeneratorUtil.getClassNameFromQualifiedName(fqn);
91          classifier.setName(name);
92          classifier.setPackage(packageName);
93          classifier.postInit(); // to create qualifiedName
94          classifier.setExtern(true);
95          cache.put(reference.getName(), classifier);
96      }
97  
98  
99  }