001/* 002 * Copyright 2011 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.ametys.cms; 018 019import javax.jcr.NoSuchWorkspaceException; 020import javax.jcr.RepositoryException; 021import javax.jcr.Session; 022 023import org.apache.avalon.framework.logger.AbstractLogEnabled; 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.avalon.framework.service.Serviceable; 027 028import org.ametys.cms.content.archive.ArchiveConstants; 029import org.ametys.plugins.repository.AmetysObjectResolver; 030import org.ametys.plugins.repository.ModifiableTraversableAmetysObject; 031import org.ametys.plugins.repository.RepositoryConstants; 032import org.ametys.plugins.repository.jcr.DefaultTraversableAmetysObject; 033import org.ametys.plugins.repository.provider.AbstractRepository; 034import org.ametys.plugins.repository.provider.JackrabbitRepository; 035 036/** 037 * CMS plugin init class. 038 */ 039public class Init extends AbstractLogEnabled implements org.ametys.runtime.plugin.Init, Serviceable 040{ 041 /** The contents root node name */ 042 private static final String __CONTENTS_ROOT_NODE = RepositoryConstants.NAMESPACE_PREFIX + ":contents"; 043 044 /** The resources root node name */ 045 private static final String __RESOURCES_ROOT_NODE = RepositoryConstants.NAMESPACE_PREFIX + ":resources"; 046 047 /** The plugins root node name */ 048 private static final String __PLUGINS_ROOT_NODE = RepositoryConstants.NAMESPACE_PREFIX + ":plugins"; 049 050 /** The repository */ 051 protected JackrabbitRepository _repository; 052 /** The Ametys object resolver */ 053 protected AmetysObjectResolver _resolver; 054 055 @Override 056 public void service(ServiceManager manager) throws ServiceException 057 { 058 _repository = (JackrabbitRepository) manager.lookup(AbstractRepository.ROLE); 059 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 060 } 061 062 @Override 063 public void init() throws Exception 064 { 065 DefaultTraversableAmetysObject root = _resolver.resolveByPath("/"); 066 067 createPluginsRootNode(root); 068 createArchiveWorkspace(); 069 070 // FIXME To move in a Init class for the project himself ? 071 createContentsRootNode(root); 072 073 // FIXME To move in a Init class for the project himself ? 074 createResourcesRootNode(root); 075 076 root.getNode().getSession().logout(); 077 } 078 079 /** 080 * Create the JCR root node for plugins storage 081 * @param rootNode The JCR root node 082 * @throws RepositoryException Thrown if the node cannot be created 083 */ 084 protected void createPluginsRootNode (ModifiableTraversableAmetysObject rootNode) throws RepositoryException 085 { 086 if (!rootNode.hasChild(__PLUGINS_ROOT_NODE)) 087 { 088 rootNode.createChild(__PLUGINS_ROOT_NODE, "ametys:unstructured"); 089 rootNode.saveChanges(); 090 } 091 } 092 093 /** 094 * Create the archive workspace. 095 * @throws RepositoryException if a repository error occurred. 096 */ 097 protected void createArchiveWorkspace() throws RepositoryException 098 { 099 Session session = null; 100 Session archiveSession = null; 101 102 try 103 { 104 // Try to login 105 archiveSession = _repository.login(ArchiveConstants.ARCHIVE_WORKSPACE); 106 } 107 catch (NoSuchWorkspaceException e) 108 { 109 getLogger().info("Archives workspace does not exist, creating it..."); 110 111 session = _repository.login(); 112 session.getWorkspace().createWorkspace(ArchiveConstants.ARCHIVE_WORKSPACE); 113 archiveSession = _repository.login(ArchiveConstants.ARCHIVE_WORKSPACE); 114 115 archiveSession.getRootNode().addNode(AmetysObjectResolver.ROOT_REPO, AmetysObjectResolver.ROOT_TYPE); 116 archiveSession.save(); 117 } 118 finally 119 { 120 if (session != null) 121 { 122 session.logout(); 123 } 124 if (archiveSession != null) 125 { 126 archiveSession.logout(); 127 } 128 } 129 } 130 131 /** 132 * Create the JCR root node for contents 133 * @param rootNode The JCR root node 134 * @throws RepositoryException if a repository error occurred. 135 */ 136 protected void createContentsRootNode (ModifiableTraversableAmetysObject rootNode) throws RepositoryException 137 { 138 if (!rootNode.hasChild(__CONTENTS_ROOT_NODE)) 139 { 140 rootNode.createChild(__CONTENTS_ROOT_NODE, "ametys:collection"); 141 rootNode.saveChanges(); 142 } 143 } 144 145 146 /** 147 * Create the JCR root node for the resources 148 * @param rootNode The JCR root node 149 * @throws RepositoryException if a repository error occurred. 150 */ 151 protected void createResourcesRootNode(ModifiableTraversableAmetysObject rootNode) throws RepositoryException 152 { 153 if (!rootNode.hasChild(__RESOURCES_ROOT_NODE)) 154 { 155 rootNode.createChild(__RESOURCES_ROOT_NODE, "ametys:resources-collection"); 156 rootNode.saveChanges(); 157 } 158 } 159 160 161 162 163}