001/* 002 * Copyright 2016 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.script; 017 018import java.util.HashMap; 019import java.util.Map; 020 021import javax.jcr.Credentials; 022import javax.jcr.LoginException; 023import javax.jcr.NoSuchWorkspaceException; 024import javax.jcr.Repository; 025import javax.jcr.RepositoryException; 026import javax.jcr.Session; 027import javax.jcr.Value; 028 029import org.apache.avalon.framework.service.ServiceException; 030import org.apache.avalon.framework.service.ServiceManager; 031 032import org.ametys.plugins.core.ui.script.ScriptExecArguments; 033import org.ametys.plugins.core.ui.script.StaticConfigurableScriptBinding; 034import org.ametys.plugins.repository.AmetysObjectResolver; 035import org.ametys.plugins.repositoryapp.RepositoryProvider; 036 037/** 038 * Script binding that provides repository variables 039 */ 040public class RepositoryScriptBinding extends StaticConfigurableScriptBinding 041{ 042 private RepositoryProvider _repositoryProvider; 043 private AmetysObjectResolver _ametysResolver; 044 045 @Override 046 public void service(ServiceManager serviceManager) throws ServiceException 047 { 048 super.service(serviceManager); 049 _repositoryProvider = (RepositoryProvider) serviceManager.lookup(RepositoryProvider.ROLE); 050 _ametysResolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE); 051 } 052 053 @Override 054 public Map<String, Object> getVariables(ScriptExecArguments execArgs) 055 { 056 Map<String, Object> variables = new HashMap<>(); 057 Repository repository = _repositoryProvider.getRepository(); 058 Credentials credentials = _repositoryProvider.getCredentials(); 059 Session session; 060 061 try 062 { 063 session = repository.login(credentials, "default"); 064 } 065 catch (Exception e) 066 { 067 getLogger().warn("Unable to provides the JavaScript console with repository variables.", e); 068 return null; 069 } 070 071 variables.put("__internal_repository_repository", new RepositoryWrapper(repository)); 072 variables.put("__internal_repository_session", session); 073 variables.put("__internal_repository_objectResolver", _ametysResolver); 074 return variables; 075 } 076 077 @Override 078 public void cleanVariables(Map<String, Object> variables) 079 { 080 if (variables.containsKey("__internal_repository_session")) 081 { 082 Object session = variables.get("__internal_repository_session"); 083 if (session != null && session instanceof Session) 084 { 085 ((Session) session).logout(); 086 variables.remove("__internal_repository_session"); 087 } 088 } 089 } 090 091 private class RepositoryWrapper implements Repository 092 { 093 private Repository _repo; 094 095 RepositoryWrapper(Repository repo) 096 { 097 _repo = repo; 098 } 099 100 @Override 101 public String getDescriptor(String key) 102 { 103 return _repo.getDescriptor(key); 104 } 105 106 @Override 107 public String[] getDescriptorKeys() 108 { 109 return _repo.getDescriptorKeys(); 110 } 111 112 @Override 113 public Value getDescriptorValue(String key) 114 { 115 return _repo.getDescriptorValue(key); 116 } 117 118 @Override 119 public Value[] getDescriptorValues(String key) 120 { 121 return _repo.getDescriptorValues(key); 122 } 123 124 @Override 125 public boolean isSingleValueDescriptor(String key) 126 { 127 return _repo.isSingleValueDescriptor(key); 128 } 129 130 @Override 131 public boolean isStandardDescriptor(String key) 132 { 133 return _repo.isStandardDescriptor(key); 134 } 135 136 @Override 137 public Session login() throws LoginException, RepositoryException 138 { 139 return _repo.login(); 140 } 141 142 @Override 143 public Session login(Credentials credentials) throws LoginException, RepositoryException 144 { 145 return _repo.login(credentials); 146 } 147 148 @Override 149 public Session login(String workspaceName) throws LoginException, NoSuchWorkspaceException, RepositoryException 150 { 151 return _repo.login(workspaceName); 152 } 153 154 @Override 155 public Session login(Credentials credentials, String workspaceName) throws LoginException, NoSuchWorkspaceException, RepositoryException 156 { 157 return _repo.login(credentials, workspaceName); 158 } 159 } 160}