View Javadoc

1   /*
2       Copyright (C) 2004 PjEr
3       
4       This program is free software; you can redistribute it and/or modify
5       it under the terms of the GNU General Public License as published by
6       the Free Software Foundation; either version 2 of the License, or
7       (at your option) any later version.
8       
9       This program is distributed in the hope that it will be useful,
10      but WITHOUT ANY WARRANTY; without even the implied warranty of
11      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12      GNU General Public License for more details.
13      
14      You should have received a copy of the GNU General Public License
15      along with this program; if not, write to the Free Software
16      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18  package ca.pjer.cm.module.metadata.defaultimpl;
19  
20  import ca.pjer.cm.api.metadata.MetaDataService;
21  import ca.pjer.cm.api.metadata.MetaData;
22  import ca.pjer.cm.api.metadata.MetaDataServiceSystemException;
23  import ca.pjer.cm.api.metadata.MetaDataNotFoundException;
24  import ca.pjer.cm.api.discovery.DiscoveryService;
25  
26  import java.util.*;
27  import java.net.URL;
28  
29  import org.apache.hivemind.Initializable;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.commons.logging.Log;
32  
33  import org.exolab.castor.mapping.Mapping;
34  import org.exolab.castor.xml.Unmarshaller;
35  import org.xml.sax.InputSource;
36  
37  /***
38   * <br />
39   * $Id: MetaDataServiceImpl.java,v 1.1 2004/05/18 23:37:14 pjer Exp $<br />
40   * <br />
41   * $RCSfile: MetaDataServiceImpl.java,v $<br />
42   * $Revision: 1.1 $<br />
43   * $Author: pjer $<br />
44   * $Date: 2004/05/18 23:37:14 $<br />
45   * 
46   * @TODO comment me.<br />
47   */
48  public class MetaDataServiceImpl implements MetaDataService, Initializable {
49      private static final Log log = LogFactory.getLog(MetaDataServiceImpl.class);
50      private static final String METADATA_MODULE = "META-INF/metadatamodule.xml";
51      private static final String METADATA_MAPPING = "ca/pjer/cm/module/metadata/defaultimpl/metadatamodule.map.xml";
52  
53      private DiscoveryService discoveryService;
54  
55      private Map metaDataMap;
56  
57      private List metaDataList;
58      private List rootMetaDataList;
59  
60      public void setDiscoveryService(DiscoveryService discoveryService) {
61          this.discoveryService = discoveryService;
62      }
63  
64      private List discoverMetaData() {
65          log.debug("discoverMetaData");
66          ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
67          Enumeration enumeration = null;
68          try {
69              enumeration = discoveryService.discover(METADATA_MODULE);
70          } catch (Exception e) {
71              String s = "Could not discover [METADATA_MODULE:" + METADATA_MODULE + "] : " + e.getMessage();
72              log.error(s);
73              throw new RuntimeException(s, e);
74          }
75          Mapping mapping = null;
76          try {
77              InputSource mappingInputSource =
78                      new InputSource(classLoader.getResourceAsStream(METADATA_MAPPING));
79              mapping = new Mapping(classLoader);
80              mapping.loadMapping(mappingInputSource);
81          } catch (Exception e) {
82              String s = "Could not load meta-data mapping : " + e.getMessage();
83              log.error(s);
84              throw new RuntimeException(s, e);
85          }
86          List metaDataList = new ArrayList();
87          try {
88              Unmarshaller unmarshaller = new Unmarshaller(mapping);
89              while (enumeration.hasMoreElements()) {
90                  URL url = (URL) enumeration.nextElement();
91                  log.debug("discovered [url:" + url + "]");
92                  InputSource metadatasInputSource = new InputSource(url.openStream());
93                  MetaDatas metaDatas = (MetaDatas) unmarshaller.unmarshal(metadatasInputSource);
94                  metaDataList.addAll(metaDatas.getMetaDataList());
95              }
96          } catch (Exception e) {
97              String s = "Could not parse meta-data descriptors : " + e.getMessage();
98              log.error(s);
99              throw new RuntimeException(s, e);
100         }
101         return metaDataList;
102     }
103 
104     public void initializeService() {
105         List discoveredMetaDataList = discoverMetaData();
106         metaDataMap = new HashMap();
107         metaDataList = new ArrayList();
108         rootMetaDataList = new ArrayList();
109         Iterator discoveredMetaDataListIterator = discoveredMetaDataList.iterator();
110         while (discoveredMetaDataListIterator.hasNext()) {
111             MetaData discoveredMetaData = (MetaData) discoveredMetaDataListIterator.next();
112             metaDataMap.put(discoveredMetaData.getClazz(), discoveredMetaData);
113             metaDataList.add(discoveredMetaData);
114             if (discoveredMetaData.getRoot().booleanValue()) {
115                 rootMetaDataList.add(discoveredMetaData);
116             }
117         }
118     }
119 
120     public MetaData get(Class clazz) throws MetaDataServiceSystemException, MetaDataNotFoundException {
121         MetaData metaData = (MetaData) metaDataMap.get(clazz);
122         if (metaData == null) {
123             throw new MetaDataNotFoundException("MetaData [clazz:" + clazz + "] not found.");
124         }
125         return metaData;
126     }
127 
128     public List list() throws MetaDataServiceSystemException {
129         return metaDataList;
130     }
131 
132     public List listRoot() throws MetaDataServiceSystemException {
133         return rootMetaDataList;
134     }
135 }