1 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 ; 16 import org.w3c.dom.NodeList ; 17 18 import java.io.InputStream ; 19 import java.util.Properties ; 20 import java.util.ArrayList ; 21 import org.exoplatform.container.configuration.ServiceConfiguration; 22 import org.exoplatform.container.configuration.ConfigurationManager; 23 import org.exoplatform.container.configuration.ValueParam; 24 25 31 public class XMLConfig implements RepositoryServiceConfig { 32 33 public static final String 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 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 cl = Thread.currentThread().getContextClassLoader(); 50 InputStream 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 is = configurationService.getInputStream((String )param.getValue()); 67 init(is); 68 69 } catch (Exception e){ 70 throw new RepositoryConfigurationException("XML config data not found! Reason: "+e ); 71 } 72 73 } 74 75 76 public XMLConfig(XMLQueryingService xmlQueryingService, InputStream source) throws RepositoryConfigurationException { 77 this.xmlQueryingService = xmlQueryingService; 78 init(source); 79 } 80 81 public String 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 repositoryName, String 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 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 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 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 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 Element defNode = (Element )selectNodes("repositories-config/default-repository").item(0); 145 if(defNode == null) 146 throw new RepositoryConfigurationException("Default node name not found in configuration."); 147 defaultRepositoryName = defNode.getAttribute("name"); 149 150 151 NodeList reps = selectNodes("repositories-config/repositories/*[name()='repository']"); 153 res = new RepositoryEntry[reps.getLength()]; 154 for (int i = 0; i < res.length; i++) { 155 Element node = (Element ) reps.item(i); 156 String manager = node.getAttribute("manager"); 157 String name = node.getAttribute("name"); 158 159 res[i] = new RepositoryEntry(name, manager); 162 163 } 164 165 166 ArrayList wsList = new ArrayList (); 167 for (int j = 0; j < res.length; j++) { 168 169 String repositoryName = res[j].getName(); 170 171 NodeList workspaces = selectNodes("repositories-config/repositories/repository[@name='" + repositoryName + "']/workspaces/*[name()='workspace']"); 173 for (int i = 0; i < workspaces.getLength(); i++) { 174 Element node = (Element ) workspaces.item(i); 175 String name = node.getAttribute("name"); 176 String container = node.getAttribute("container"); 177 String 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 reps = selectNodes("repositories-config/containers/*"); 193 ces = new ContainerEntry[reps.getLength()]; 194 for (int i = 0; i < ces.length; i++) { 195 Properties props = new Properties (); 196 Element node = (Element ) reps.item(i); 197 String name = node.getAttribute("name"); 198 Class clazz = null; 199 try { 200 clazz = Class.forName(node.getAttribute("class")); 201 } catch (Exception e) { 202 } 203 204 NodeList ps = selectNodes("repositories-config/containers/container[@name='" + 206 name + "']/properties/*"); 207 for (int j = 0; j < ps.getLength(); j++) { 208 Element p = (Element ) ps.item(j); 209 props.setProperty(p.getAttribute("name"), p.getAttribute("value")); 210 } 211 ces[i] = new ContainerEntry(name, clazz, props); 212 } 213 214 reps = selectNodes("repositories-config/managers/*"); 216 mes = new RepositoryManagerEntry[reps.getLength()]; 217 for (int i = 0; i < mes.length; i++) { 218 Properties props = new Properties (); 219 Element node = (Element ) reps.item(i); 220 String name = node.getAttribute("name"); 221 Class clazz = null; 222 try { 223 clazz = Class.forName(node.getAttribute("class")); 224 } catch (Exception e) { 225 } 226 227 NodeList ps = selectNodes("repositories-config/managers/manager[@name='" + 229 name + "']/properties/*"); 230 for (int j = 0; j < ps.getLength(); j++) { 231 Element p = (Element ) 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 e) { 238 throw new RepositoryConfigurationException("XMLConfig failed. Reason " + e); 239 } 240 } 241 242 private NodeList selectNodes(String xpath) throws Exception { 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 |