1 13 package info.magnolia.cms.beans.config; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.core.HierarchyManager; 17 import info.magnolia.cms.core.Path; 18 import info.magnolia.cms.core.SystemProperty; 19 import info.magnolia.cms.core.search.QueryManager; 20 import info.magnolia.cms.core.search.SearchFactory; 21 import info.magnolia.cms.security.AccessDeniedException; 22 import info.magnolia.cms.security.AccessManagerImpl; 23 import info.magnolia.cms.security.Permission; 24 import info.magnolia.cms.security.PermissionImpl; 25 import info.magnolia.cms.util.ClassUtil; 26 import info.magnolia.cms.util.ConfigUtil; 27 import info.magnolia.cms.util.UrlPattern; 28 import info.magnolia.repository.Provider; 29 import info.magnolia.repository.RepositoryMapping; 30 import info.magnolia.repository.RepositoryNotInitializedException; 31 import info.magnolia.repository.RepositoryNameMap; 32 33 import java.io.File ; 34 import java.io.FileInputStream ; 35 import java.io.FileNotFoundException ; 36 import java.io.IOException ; 37 import java.util.ArrayList ; 38 import java.util.Collection ; 39 import java.util.Hashtable ; 40 import java.util.Iterator ; 41 import java.util.List ; 42 import java.util.Map ; 43 44 import javax.jcr.Repository; 45 import javax.jcr.RepositoryException; 46 import javax.jcr.Session; 47 import javax.jcr.SimpleCredentials; 48 49 import org.apache.commons.lang.BooleanUtils; 50 import org.apache.commons.lang.StringUtils; 51 import org.jdom.Document; 52 import org.jdom.Element; 53 import org.jdom.JDOMException; 54 import org.slf4j.Logger; 55 import org.slf4j.LoggerFactory; 56 57 58 62 public final class ContentRepository { 63 64 67 public static final String WEBSITE = "website"; 69 public static final String USERS = "users"; 71 public static final String USER_ROLES = "userroles"; 73 public static final String USER_GROUPS = "usergroups"; 75 public static final String CONFIG = "config"; 77 public static final String DEFAULT_WORKSPACE = "default"; 79 public static final String VERSION_STORE = "mgnlVersion"; 80 81 84 public static final String NAMESPACE_PREFIX = "mgnl"; 86 public static final String NAMESPACE_URI = "http://www.magnolia.info/jcr/mgnl"; 88 91 private static Logger log = LoggerFactory.getLogger(ContentRepository.class); 92 93 96 private static final String ELEMENT_REPOSITORY = "Repository"; 98 private static final String ELEMENT_REPOSITORYMAPPING = "RepositoryMapping"; 100 private static final String ELEMENT_PARAM = "param"; 102 private static final String ELEMENT_WORKSPACE = "workspace"; 104 107 private static final String ATTRIBUTE_NAME = "name"; 109 private static final String ATTRIBUTE_LOAD_ON_STARTUP = "loadOnStartup"; 111 private static final String ATTRIBUTE_PROVIDER = "provider"; 113 private static final String ATTRIBUTE_VALUE = "value"; 115 private static final String ATTRIBUTE_REPOSITORY_NAME = "repositoryName"; 117 private static final String ATTRIBUTE_WORKSPACE_NAME = "workspaceName"; 119 122 public static final String REPOSITORY_USER = SystemProperty.getProperty("magnolia.connection.jcr.userId"); 123 124 127 public static final String REPOSITORY_PSWD = SystemProperty.getProperty("magnolia.connection.jcr.password"); 128 129 132 private static Map repositories = new Hashtable (); 133 134 137 private static Map repositoryProviders = new Hashtable (); 138 139 142 private static Map hierarchyManagers = new Hashtable (); 143 144 148 private static Map repositoryMapping = new Hashtable (); 149 150 153 private static Map repositoryNameMap = new Hashtable (); 154 155 158 private ContentRepository() { 159 } 161 162 182 public static void init() { 183 log.info("System : loading JCR"); repositories.clear(); 185 hierarchyManagers.clear(); 186 try { 187 loadRepositories(); 188 log.info("System : JCR loaded"); } 190 catch (Exception e) { 191 log.error(e.getMessage(), e); 192 } 193 } 194 195 203 public static boolean checkIfInitialized() throws AccessDeniedException, RepositoryException { 204 Iterator repositoryNames = getAllRepositoryNames(); 205 while (repositoryNames.hasNext()) { 206 String repository = (String ) repositoryNames.next(); 207 if (checkIfInitialized(repository)) { 208 return true; 209 } 210 } 211 return false; 212 } 213 214 219 public static boolean checkIfInitialized(String repository) throws RepositoryException, AccessDeniedException { 220 if (log.isDebugEnabled()) { 221 log.debug("Checking [" + repository + "] repository."); } 223 HierarchyManager hm = getHierarchyManager(repository); 224 225 if (hm == null) { 226 throw new RuntimeException ("Repository [" + repository + "] not loaded"); } 228 229 Content startPage = hm.getRoot(); 230 231 Collection children = startPage.getChildren(new Content.ContentFilter(){ 233 public boolean accept(Content content) { 234 return !content.getName().startsWith("jcr:"); 235 } 236 }); 237 238 if (!children.isEmpty()) { 239 if (log.isDebugEnabled()) { 240 log.debug("Content found in [" + repository + "]."); } 242 return true; 243 } 244 return false; 245 } 246 247 251 public static void reload() { 252 log.info("System : reloading JCR"); ContentRepository.init(); 254 } 255 256 260 private static void loadRepositories() throws Exception { 261 Document document = buildDocument(); 262 Element root = document.getRootElement(); 263 loadRepositoryNameMap(root); 264 Collection repositoryElements = root.getChildren(ContentRepository.ELEMENT_REPOSITORY); 265 Iterator children = repositoryElements.iterator(); 266 while (children.hasNext()) { 267 Element element = (Element) children.next(); 268 String name = element.getAttributeValue(ATTRIBUTE_NAME); 269 String load = element.getAttributeValue(ATTRIBUTE_LOAD_ON_STARTUP); 270 String provider = element.getAttributeValue(ATTRIBUTE_PROVIDER); 271 RepositoryMapping map = new RepositoryMapping(); 272 map.setName(name); 273 map.setProvider(provider); 274 boolean loadOnStartup = BooleanUtils.toBoolean(load); 275 map.setLoadOnStartup(loadOnStartup); 276 277 Iterator params = element.getChildren(ELEMENT_PARAM).iterator(); 278 Map parameters = new Hashtable (); 279 while (params.hasNext()) { 280 Element param = (Element) params.next(); 281 String value = param.getAttributeValue(ATTRIBUTE_VALUE); 282 parameters.put(param.getAttributeValue(ATTRIBUTE_NAME), value); 283 } 284 map.setParameters(parameters); 285 List workspaces = element.getChildren(ELEMENT_WORKSPACE); 286 if (workspaces != null && !workspaces.isEmpty()) { 287 Iterator wspIterator = workspaces.iterator(); 288 while (wspIterator.hasNext()) { 289 Element workspace = (Element) wspIterator.next(); 290 String wspName = workspace.getAttributeValue(ATTRIBUTE_NAME); 291 log.info("Loading workspace:" + wspName); 292 map.addWorkspace(wspName); 293 } 294 } 295 else { 296 map.addWorkspace(DEFAULT_WORKSPACE); 297 } 298 ContentRepository.repositoryMapping.put(name, map); 299 try { 300 loadRepository(map); 301 } 302 catch (Exception e) { 303 log.error("System : Failed to load JCR \"" + map.getName() + "\" " + e.getMessage(), e); } 305 } 306 } 307 308 312 private static void loadRepositoryNameMap(Element root) { 313 Element repositoryMapping = root.getChild(ContentRepository.ELEMENT_REPOSITORYMAPPING); 314 Iterator children = repositoryMapping.getChildren().iterator(); 315 while (children.hasNext()) { 316 Element nameMap = (Element) children.next(); 317 addMappedRepositoryName( 318 nameMap.getAttributeValue(ATTRIBUTE_NAME), 319 nameMap.getAttributeValue(ATTRIBUTE_REPOSITORY_NAME), 320 nameMap.getAttributeValue(ATTRIBUTE_WORKSPACE_NAME)); 321 } 322 } 323 324 332 public static void loadRepository(RepositoryMapping map) throws RepositoryNotInitializedException, 333 InstantiationException , IllegalAccessException , ClassNotFoundException { 334 log.info("System : loading JCR {}", map.getName()); Provider handlerClass = (Provider) ClassUtil.newInstance(map.getProvider()); 336 handlerClass.init(map); 337 Repository repository = handlerClass.getUnderlineRepository(); 338 ContentRepository.repositories.put(map.getName(), repository); 339 ContentRepository.repositoryProviders.put(map.getName(), handlerClass); 340 if (map.isLoadOnStartup()) { 341 Iterator workspaces = map.getWorkspaces().iterator(); 343 while (workspaces.hasNext()) { 344 String wspID = (String ) workspaces.next(); 345 loadHierarchyManager(repository, wspID, map, handlerClass); 346 } 347 } 348 } 349 350 355 public static void loadWorkspace(String repositoryId, String workspaceId) throws RepositoryException { 356 log.info("System : loading workspace {}", workspaceId); if(!repositoryNameMap.containsKey(workspaceId)){ 358 addMappedRepositoryName(workspaceId, repositoryId, workspaceId); 359 } 360 RepositoryMapping map = getRepositoryMapping(repositoryId); 361 if(!map.getWorkspaces().contains(workspaceId)){ 362 map.addWorkspace(workspaceId); 363 } 364 Provider provider = getRepositoryProvider(repositoryId); 365 provider.registerWorkspace(workspaceId); 366 loadHierarchyManager(getRepository(repositoryId), workspaceId, map, provider); 367 } 368 369 376 private static void loadHierarchyManager(Repository repository, String wspID, RepositoryMapping map, 377 Provider provider) { 378 try { 379 SimpleCredentials sc = new SimpleCredentials(REPOSITORY_USER, REPOSITORY_PSWD.toCharArray()); 380 Session session = repository.login(sc, wspID); 381 provider.registerNamespace(NAMESPACE_PREFIX, NAMESPACE_URI, session.getWorkspace()); 382 provider.registerNodeTypes(); 383 AccessManagerImpl accessManager = getAccessManager(); 384 HierarchyManager hierarchyManager = new HierarchyManager(REPOSITORY_USER); 385 hierarchyManager.init(session.getRootNode()); 386 hierarchyManager.setAccessManager(accessManager); 387 ContentRepository.hierarchyManagers.put(map.getName() 388 + "_" 389 + getInternalWorkspaceName(wspID), hierarchyManager); try { 391 QueryManager queryManager = SearchFactory.getAccessControllableQueryManager(hierarchyManager 392 .getWorkspace() 393 .getQueryManager(), accessManager); 394 hierarchyManager.setQueryManager(queryManager); 395 } 396 catch (RepositoryException e) { 397 log.info("QueryManager not initialized for repository {}: {}", map.getName(), e.getMessage()); } 400 } 401 catch (RepositoryException re) { 402 log.error("System : Failed to initialize hierarchy manager for JCR {}", map.getName()); log.error(re.getMessage(), re); 404 } 405 } 406 407 411 public static AccessManagerImpl getAccessManager() { 412 List acl = getSystemPermissions(); 413 AccessManagerImpl accessManager = new AccessManagerImpl(); 414 accessManager.setPermissionList(acl); 415 return accessManager; 416 } 417 418 422 private static List getSystemPermissions() { 423 List acl = new ArrayList (); 424 UrlPattern p = UrlPattern.MATCH_ALL; 425 Permission permission = new PermissionImpl(); 426 permission.setPattern(p); 427 permission.setPermissions(Permission.ALL); 428 acl.add(permission); 429 return acl; 430 } 431 432 438 private static Document buildDocument() throws JDOMException, IOException { 439 File source = Path.getRepositoriesConfigFile(); 440 if (!source.exists()) { 441 throw new FileNotFoundException ("Failed to locate magnolia repositories config file at " + source.getAbsolutePath()); 443 } 444 445 return ConfigUtil.string2JDOM(ConfigUtil.replaceTokens(new FileInputStream (source))); 446 } 447 448 453 public static String getMappedRepositoryName(String name) { 454 RepositoryNameMap nameMap = (RepositoryNameMap) ContentRepository.repositoryNameMap.get(name); 455 if(nameMap==null){ 456 return name; 457 } 458 return nameMap.getRepositoryName(); 459 } 460 461 466 public static String getMappedWorkspaceName(String name) { 467 RepositoryNameMap nameMap = (RepositoryNameMap) ContentRepository.repositoryNameMap.get(name); 468 return nameMap.getWorkspaceName(); 469 } 470 471 476 public static void addMappedRepositoryName(String name, String repositoryName) { 477 addMappedRepositoryName(name, repositoryName, null); 478 } 479 480 486 public static void addMappedRepositoryName(String name, String repositoryName, String workspaceName) { 487 if (StringUtils.isEmpty(workspaceName)) workspaceName = name; 488 RepositoryNameMap nameMap = new RepositoryNameMap(repositoryName, workspaceName); 489 ContentRepository.repositoryNameMap.put(name, nameMap); 490 } 491 492 497 public static String getDefaultWorkspace(String repositoryId) { 498 RepositoryMapping mapping = getRepositoryMapping(repositoryId); 499 if (mapping == null) { 500 return DEFAULT_WORKSPACE; 501 } 502 Collection workspaces = mapping.getWorkspaces(); 503 if (workspaces.contains(getMappedWorkspaceName(repositoryId))) { 504 return repositoryId; 505 } 506 return DEFAULT_WORKSPACE; 507 } 508 509 513 public static HierarchyManager getHierarchyManager(String repositoryID) { 514 return getHierarchyManager(repositoryID, getDefaultWorkspace(repositoryID)); 515 } 516 517 521 public static HierarchyManager getHierarchyManager(String repositoryID, String workspaceID) { 522 String mappedRepositoryName = getMappedRepositoryName(repositoryID); 523 if (mappedRepositoryName == null) { 524 return null; 525 } 526 return (HierarchyManager) ContentRepository.hierarchyManagers.get(mappedRepositoryName + '_' + workspaceID); 527 } 528 529 532 public static Repository getRepository(String repositoryID) { 533 String mappedRepositoryName = getMappedRepositoryName(repositoryID); 534 if (mappedRepositoryName == null) { 535 return null; 536 } 537 return (Repository) ContentRepository.repositories.get(mappedRepositoryName); 538 } 539 540 543 public static Provider getRepositoryProvider(String repositoryID) { 544 545 Provider provider = (Provider) ContentRepository.repositoryProviders.get(repositoryID); 546 547 if (provider == null) { 548 String mappedRepositoryName = getMappedRepositoryName(repositoryID); 549 if (mappedRepositoryName != null) { 550 provider = (Provider) ContentRepository.repositoryProviders.get(mappedRepositoryName); 551 } 552 } 553 return provider; 554 } 555 556 559 public static RepositoryMapping getRepositoryMapping(String repositoryID) { 560 String name = getMappedRepositoryName(repositoryID); 561 if (name != null && ContentRepository.repositoryMapping.containsKey(name)) { 562 return (RepositoryMapping) ContentRepository.repositoryMapping.get(getMappedRepositoryName(repositoryID)); 563 } 564 log.warn("no mapping for the repository [" + repositoryID + "]"); 565 return null; 566 } 567 568 571 public static boolean hasRepositoryMapping(String repositoryID) { 572 String name = getMappedRepositoryName(repositoryID); 573 return name != null; 574 } 575 576 580 public static Iterator getAllRepositoryNames() { 581 return ContentRepository.repositoryNameMap.keySet().iterator(); 582 } 583 584 589 private static String getInternalWorkspaceName(String workspaceName) { 590 Iterator keys = repositoryNameMap.keySet().iterator(); 591 while (keys.hasNext()) { 592 String key = (String ) keys.next(); 593 RepositoryNameMap nameMap = (RepositoryNameMap) repositoryNameMap.get(key); 594 if (nameMap.getWorkspaceName().equalsIgnoreCase(workspaceName)) return key; 595 } 596 log.error("No Repository/Workspace name mapping defined for "+workspaceName); 597 return workspaceName; 598 } 599 600 } 601 | Popular Tags |