View Javadoc

1   package org.weda.store.impl;
2   
3   import java.util.Map;
4   import org.apache.commons.logging.Log;
5   import org.apache.commons.logging.LogFactory;
6   import org.weda.action.ActionRegistry;
7   import org.weda.message.Messages;
8   import org.weda.message.MessagesRegistry;
9   import org.weda.property.PropertyValue;
10  import org.weda.cache.CacheEntity;
11  import org.weda.converter.ValueTypeConverter;
12  import org.weda.store.ObjectSourceException;
13  import org.weda.store.ObjectSourceRegistryException;
14  import org.weda.store.DetailObjectSourceInfo;
15  import org.weda.cache.CacheManager;
16  import org.weda.store.ObjectSource;
17  import org.weda.store.ObjectSourceRegistry;
18  import org.weda.property.ObjectDescriptorRegistry;
19  import org.weda.store.ObjectStore;
20  import org.weda.message.impl.MessagesSubset;
21  
22  /**Цель: предотавление доступа к {@link tim.docarch.service.DataSource}.
23   *
24   * @author Mikhail Titov
25   */
26  public class ObjectSourceRegistryImpl implements ObjectSourceRegistry{
27      private final static Log log = 
28              LogFactory.getLog(ObjectSourceRegistryImpl.class);
29      private CacheManager cacheManager;
30      private Map<String, ObjectSource> config;
31      
32      public void init() throws ObjectSourceRegistryException {
33          try{
34              for (ObjectSource dataSource: config.values()){
35                  dataSource.init();
36                  if (dataSource.getDetailInfos()!=null){
37                      for (DetailObjectSourceInfo detailInfo: 
38                              dataSource.getDetailInfos())
39                      {
40                          if (!config.containsKey(detailInfo.getName()))
41                              throw new ObjectSourceRegistryException(
42                                  String.format(
43                                      "Can't resolve detail dataSource (%2$s) " +
44                                      "for master dataSource (%1$s). " +
45                                      "DataSource (%2$s) not found."
46                                      , dataSource.getName()
47                                      , detailInfo.getName()));
48                      }
49                  }
50              }
51              
52          }catch(ObjectSourceException e){
53              throw new ObjectSourceRegistryException(                    
54                      "Error in initializing dataSourceRegistry service"
55                      , e);
56          }
57      }
58      /**Метод возвращает ObjectSource по его имени.
59       */
60      public ObjectSource getObjectSource(String name) throws Exception {
61          ObjectSourceCacheEntity entity=
62                  (ObjectSourceCacheEntity)
63                      cacheManager.getCacheService().get(name);
64          ObjectSource objectSource=null;
65          if (entity==null){
66              String objectSourceName = name;
67              int pos = name.indexOf('#');
68              if (pos >= 0){
69                  objectSourceName = name.substring(0, pos);
70                  if (pos+1 == name.length())
71                      throw new ObjectSourceRegistryException(
72                              String.format(
73                                  "Additional identificator for object source " +
74                                  "can not be empty. Object source name is (%s)"
75                                  , objectSourceName));
76              }
77              objectSource = config.get(objectSourceName);
78              if (objectSource==null)
79                  throw new ObjectSourceRegistryException(
80                          String.format(
81                              "Registry doesn't contains object source (%s)"
82                              , objectSourceName));
83              objectSource = (ObjectSource)objectSource.clone();
84              objectSource.init();
85              cacheManager.getCacheService()
86                  .setCacheEntity(
87                      name, new ObjectSourceCacheEntity(objectSource));
88          }else
89              objectSource = entity.getObjectSource();
90          return objectSource;
91      }
92  
93      public void setCacheManager(CacheManager cacheManager) {
94          this.cacheManager = cacheManager;
95      }
96  
97      public void setConfig(Map<String, ObjectSource> config) throws Exception {
98          this.config = config;
99      }
100 
101     private class ObjectSourceCacheEntity implements CacheEntity {
102         private ObjectSource dataSource;
103         
104         public ObjectSourceCacheEntity(ObjectSource dataSource){
105             this.dataSource=dataSource;
106         }
107         
108         public void release() {
109         }
110 
111         public Object getValue() {
112             return dataSource;
113         }
114         
115         public ObjectSource getObjectSource(){
116             return dataSource;
117         }
118         
119     }
120 
121 }