001/* 002 * Copyright 2010 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 */ 016package org.ametys.plugins.repository.provider; 017 018import javax.jcr.Repository; 019import javax.jcr.RepositoryException; 020import javax.jcr.Session; 021 022import org.apache.avalon.framework.context.Context; 023import org.apache.avalon.framework.context.ContextException; 024import org.apache.avalon.framework.context.Contextualizable; 025import org.apache.avalon.framework.logger.LogEnabled; 026import org.apache.avalon.framework.logger.Logger; 027import org.apache.avalon.framework.service.ServiceException; 028import org.apache.avalon.framework.service.ServiceManager; 029import org.apache.avalon.framework.service.Serviceable; 030import org.apache.cocoon.components.ContextHelper; 031import org.apache.cocoon.environment.Request; 032 033import org.ametys.plugins.repository.RepositoryConstants; 034 035/** 036 * Workspace selector using a context attribute for selecting 037 * the workspace. 038 */ 039public class RequestAttributeWorkspaceSelector implements LogEnabled, Contextualizable, WorkspaceSelector, Serviceable 040{ 041 private static final String __WORKSPACE_ATTRIBUTE = "jcr-workspace"; 042 043 /** The repository. */ 044 protected Repository _repository; 045 046 private Logger _logger; 047 private Context _context; 048 049 @Override 050 public void service(ServiceManager smanager) throws ServiceException 051 { 052 _repository = (Repository) smanager.lookup(AbstractRepository.ROLE); 053 } 054 055 @Override 056 public void contextualize(Context context) throws ContextException 057 { 058 _context = context; 059 } 060 061 @Override 062 public void enableLogging(Logger logger) 063 { 064 _logger = logger; 065 } 066 067 /** 068 * Forces the workspace to use. 069 * @param request the current request 070 * @param workspaceName the workspace name. 071 */ 072 public static void setForcedWorkspace(Request request, String workspaceName) 073 { 074 request.setAttribute(__WORKSPACE_ATTRIBUTE, workspaceName); 075 } 076 077 /** 078 * Retrieves the workspace forced. 079 * @param request the current request 080 * @return the workspace forced or <code>null</code> for the default one. 081 */ 082 public static String getForcedWorkspace(Request request) 083 { 084 return (String) request.getAttribute(__WORKSPACE_ATTRIBUTE); 085 } 086 087 @Override 088 public String getWorkspace() 089 { 090 String workspace; 091 try 092 { 093 Request request = (Request) _context.get(ContextHelper.CONTEXT_REQUEST_OBJECT); 094 workspace = getForcedWorkspace(request); 095 } 096 catch (ContextException e) 097 { 098 // no current request 099 if (_logger.isDebugEnabled()) 100 { 101 _logger.debug("No current request, using default workspace"); 102 } 103 104 return RepositoryConstants.DEFAULT_WORKSPACE; 105 } 106 107 if (workspace != null) 108 { 109 if (_logger.isDebugEnabled()) 110 { 111 _logger.debug("Using workspace: " + workspace); 112 } 113 114 return workspace; 115 } 116 117 if (_logger.isDebugEnabled()) 118 { 119 _logger.debug("Using default workspace"); 120 } 121 122 // No value, fall back to the default workspace 123 return RepositoryConstants.DEFAULT_WORKSPACE; 124 } 125 126 public String[] getWorkspaces() throws RepositoryException 127 { 128 Session session = _repository.login(); 129 return session.getWorkspace().getAccessibleWorkspaceNames(); 130 } 131}