KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > impl > config > XMLConfig


1 /**
2  **************************************************************************
3  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
4  * Please look at license.txt in info directory for more license detail. *
5  */

6
7 package org.exoplatform.services.jcr.impl.config;
8
9 import org.exoplatform.services.jcr.config.*;
10 import org.exoplatform.services.xml.querying.Statement;
11 import org.exoplatform.services.xml.querying.XMLQuery;
12 import org.exoplatform.services.xml.querying.XMLQueryingService;
13 import org.exoplatform.services.xml.querying.helper.SimpleStatementHelper;
14 import org.exoplatform.services.xml.querying.helper.XMLDataManager;
15 import org.w3c.dom.Element JavaDoc;
16 import org.w3c.dom.NodeList JavaDoc;
17
18 import java.io.InputStream JavaDoc;
19 import java.util.Properties JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import org.exoplatform.container.configuration.ServiceConfiguration;
22 import org.exoplatform.container.configuration.ConfigurationManager;
23 import org.exoplatform.container.configuration.ValueParam;
24
25 /**
26  * Created by The eXo Platform SARL
27  *
28  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
29  * @version $Id: XMLConfig.java,v 1.11 2004/09/22 13:54:54 geaz Exp $
30  */

31 public class XMLConfig implements RepositoryServiceConfig {
32
33   public static final String JavaDoc CONF_FILE = "conf/portal/exo-jcr-config.xml";
34
35   private WorkspaceEntry[] wes;
36   private ContainerEntry[] ces;
37   private RepositoryEntry[] res;
38   private RepositoryManagerEntry[] mes;
39   private String JavaDoc defaultRepositoryName;
40
41   private XMLQueryingService xmlQueryingService;
42   private SimpleStatementHelper sHelper;
43   private XMLDataManager dManager;
44   private XMLQuery query;
45
46   public XMLConfig(XMLQueryingService xmlQueryingService) throws RepositoryConfigurationException {
47     this.xmlQueryingService = xmlQueryingService;
48
49     ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
50     InputStream JavaDoc config = cl.getResourceAsStream(CONF_FILE);
51
52     if (config == null)
53       throw new RepositoryConfigurationException("XML config data not found! Check if '" + CONF_FILE + "' file exists.");
54
55     init(config);
56   }
57
58   public XMLConfig(XMLQueryingService xmlQueryingService, ConfigurationManager configurationService) throws RepositoryConfigurationException {
59
60     this.xmlQueryingService = xmlQueryingService;
61     try {
62
63         ServiceConfiguration conf =
64              configurationService.getServiceConfiguration(XMLConfig.class);
65         ValueParam param = conf.getValueParam("conf-path");
66         InputStream JavaDoc is = configurationService.getInputStream((String JavaDoc)param.getValue());
67         init(is);
68
69     } catch (Exception JavaDoc e){
70       throw new RepositoryConfigurationException("XML config data not found! Reason: "+e );
71     }
72
73   }
74
75
76   public XMLConfig(XMLQueryingService xmlQueryingService, InputStream JavaDoc source) throws RepositoryConfigurationException {
77     this.xmlQueryingService = xmlQueryingService;
78     init(source);
79   }
80
81   public String JavaDoc getDefaultRepositoryName() {
82     return defaultRepositoryName;
83   }
84
85
86   public RepositoryEntry[] getRepositoryEntries() {
87     return res;
88   }
89
90   public WorkspaceEntry[] getWorkspaceEntries() {
91     return wes;
92   }
93
94   public WorkspaceEntry getWorkspaceEntry(String JavaDoc repositoryName, String JavaDoc name) {
95     for (int i = 0; i < wes.length; i++) {
96       if (wes[i].getRepositoryName().equals(repositoryName) && wes[i].getName().equals(name))
97         return wes[i];
98     }
99     return null;
100   }
101
102   public ContainerEntry[] getSupportedContainerEntries() {
103     return ces;
104   }
105
106   public ContainerEntry getContainerEntry(String JavaDoc name) {
107     for (int i = 0; i < ces.length; i++) {
108       if (ces[i].getName().equals(name))
109         return ces[i];
110     }
111     return null;
112   }
113
114   public RepositoryManagerEntry[] getSupportedRepositoryManagerEntries() {
115     return mes;
116   }
117
118   public RepositoryManagerEntry getRepositoryManagerEntry(String JavaDoc name) {
119     for (int i = 0; i < mes.length; i++) {
120       if (mes[i].getName().equals(name))
121         return mes[i];
122     }
123     return null;
124   }
125
126   public RepositoryEntry getRepositoryEntry(String JavaDoc name) {
127     for (int i = 0; i < res.length; i++) {
128       if (res[i].getName().equals(name))
129         return res[i];
130     }
131     return null;
132   }
133
134   private void init(InputStream JavaDoc source) throws RepositoryConfigurationException {
135
136     try {
137
138       sHelper = xmlQueryingService.createStatementHelper();
139       dManager = xmlQueryingService.createXMLDataManager();
140       query = xmlQueryingService.createQuery();
141       query.setInputStream(source);
142
143       // default-repository
144
Element JavaDoc defNode = (Element JavaDoc)selectNodes("repositories-config/default-repository").item(0);
145       if(defNode == null)
146         throw new RepositoryConfigurationException("Default node name not found in configuration.");
147 // Element node = (Element) reps.item(0);
148
defaultRepositoryName = defNode.getAttribute("name");
149
150
151       // repositories
152
NodeList JavaDoc reps = selectNodes("repositories-config/repositories/*[name()='repository']");
153       res = new RepositoryEntry[reps.getLength()];
154       for (int i = 0; i < res.length; i++) {
155         Element JavaDoc node = (Element JavaDoc) reps.item(i);
156         String JavaDoc manager = node.getAttribute("manager");
157         String JavaDoc name = node.getAttribute("name");
158
159 // if (alias.equals(""))
160
// alias = cAlias;
161
res[i] = new RepositoryEntry(name, manager);
162
163       }
164
165
166       ArrayList JavaDoc wsList = new ArrayList JavaDoc();
167       for (int j = 0; j < res.length; j++) {
168
169          String JavaDoc repositoryName = res[j].getName();
170
171          // workspaces
172
NodeList JavaDoc workspaces = selectNodes("repositories-config/repositories/repository[@name='" + repositoryName + "']/workspaces/*[name()='workspace']");
173          for (int i = 0; i < workspaces.getLength(); i++) {
174            Element JavaDoc node = (Element JavaDoc) workspaces.item(i);
175            String JavaDoc name = node.getAttribute("name");
176            String JavaDoc container = node.getAttribute("container");
177            String JavaDoc defaultWorkspace = node.getAttribute("default");
178            if ("true".equals(defaultWorkspace))
179              wsList.add(new WorkspaceEntry(name, repositoryName, true, container));
180            else if ("false".equals(defaultWorkspace))
181              wsList.add(new WorkspaceEntry(name, repositoryName, false, container));
182            else
183              wsList.add(new WorkspaceEntry(name, repositoryName, container));
184          }
185       }
186
187       wes = new WorkspaceEntry[wsList.size()];
188       for(int i=0; i<wsList.size(); i++)
189         wes[i] = (WorkspaceEntry)wsList.get(i);
190
191       // ws containers
192
reps = selectNodes("repositories-config/containers/*");
193       ces = new ContainerEntry[reps.getLength()];
194       for (int i = 0; i < ces.length; i++) {
195         Properties JavaDoc props = new Properties JavaDoc();
196         Element JavaDoc node = (Element JavaDoc) reps.item(i);
197         String JavaDoc name = node.getAttribute("name");
198         Class JavaDoc clazz = null;
199         try {
200           clazz = Class.forName(node.getAttribute("class"));
201         } catch (Exception JavaDoc e) {
202         }
203
204         // ws containers' properties
205
NodeList JavaDoc ps = selectNodes("repositories-config/containers/container[@name='" +
206             name + "']/properties/*");
207         for (int j = 0; j < ps.getLength(); j++) {
208           Element JavaDoc p = (Element JavaDoc) ps.item(j);
209           props.setProperty(p.getAttribute("name"), p.getAttribute("value"));
210         }
211         ces[i] = new ContainerEntry(name, clazz, props);
212       }
213
214       // repository managers
215
reps = selectNodes("repositories-config/managers/*");
216       mes = new RepositoryManagerEntry[reps.getLength()];
217       for (int i = 0; i < mes.length; i++) {
218         Properties JavaDoc props = new Properties JavaDoc();
219         Element JavaDoc node = (Element JavaDoc) reps.item(i);
220         String JavaDoc name = node.getAttribute("name");
221         Class JavaDoc clazz = null;
222         try {
223           clazz = Class.forName(node.getAttribute("class"));
224         } catch (Exception JavaDoc e) {
225         }
226
227         // repository managers' properties
228
NodeList JavaDoc ps = selectNodes("repositories-config/managers/manager[@name='" +
229                                   name + "']/properties/*");
230         for (int j = 0; j < ps.getLength(); j++) {
231           Element JavaDoc p = (Element JavaDoc) ps.item(j);
232           props.setProperty(p.getAttribute("name"), p.getAttribute("value"));
233         }
234         mes[i] = new RepositoryManagerEntry(name, clazz, props);
235       }
236
237     } catch (Exception JavaDoc e) {
238       throw new RepositoryConfigurationException("XMLConfig failed. Reason " + e);
239     }
240   }
241
242   private NodeList JavaDoc selectNodes(String JavaDoc xpath) throws Exception JavaDoc {
243       Statement stat = sHelper.select(xpath);
244
245       query.prepare(stat);
246       query.execute();
247       return dManager.toFragment(query.getResult()).getAsNodeList();
248   }
249 }
250
Popular Tags