2 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the GNU Lesser General Public License
\r
6 * (LGPL) version 2.1 which accompanies this distribution, and is available at
\r
7 * http://www.gnu.org/licenses/lgpl.html
\r
9 * This library is distributed in the hope that it will be useful,
\r
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
12 * Lesser General Public License for more details.
\r
15 * bstefanescu, jcarsique
\r
20 package org.collectionspace.services.nuxeo.client.java;
\r
22 import java.util.Collection;
\r
23 import java.util.HashMap;
\r
24 import java.util.Iterator;
\r
25 import java.util.Map;
\r
26 import java.util.Map.Entry;
\r
28 import javax.security.auth.login.AppConfigurationEntry;
\r
29 import javax.transaction.TransactionManager;
\r
31 import org.apache.commons.logging.Log;
\r
32 import org.apache.commons.logging.LogFactory;
\r
33 import org.collectionspace.services.config.tenant.RepositoryDomainType;
\r
34 import org.jboss.remoting.InvokerLocator;
\r
35 import org.nuxeo.common.collections.ListenerList;
\r
36 import org.nuxeo.ecm.core.api.repository.Repository;
\r
37 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
\r
38 import org.nuxeo.ecm.core.api.repository.RepositoryInstanceHandler;
\r
39 import org.nuxeo.ecm.core.api.repository.RepositoryManager;
\r
40 import org.nuxeo.ecm.core.client.ConnectionListener;
\r
41 import org.nuxeo.ecm.core.client.DefaultLoginHandler;
\r
42 import org.nuxeo.ecm.core.client.LoginHandler;
\r
43 //import org.nuxeo.ecm.core.repository.RepositoryDescriptor;
\r
44 import org.nuxeo.ecm.core.schema.SchemaManager;
\r
45 import org.nuxeo.ecm.core.schema.SchemaManagerImpl;
\r
46 import org.nuxeo.ecm.core.schema.TypeProvider;
\r
47 import org.nuxeo.runtime.api.Framework;
\r
48 import org.nuxeo.runtime.api.ServiceDescriptor;
\r
49 import org.nuxeo.runtime.api.ServiceManager;
\r
50 import org.nuxeo.runtime.api.login.LoginComponent;
\r
51 import org.nuxeo.runtime.api.login.LoginService;
\r
52 import org.nuxeo.runtime.api.login.SecurityDomain;
\r
53 import org.nuxeo.runtime.config.AutoConfigurationService;
\r
54 import org.nuxeo.runtime.remoting.RemotingService;
\r
55 import org.nuxeo.runtime.services.streaming.StreamingService;
\r
56 import org.nuxeo.runtime.transaction.TransactionHelper;
\r
57 import org.slf4j.Logger;
\r
58 import org.slf4j.LoggerFactory;
\r
61 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
\r
64 public final class NuxeoClientEmbedded {
\r
66 private Logger logger = LoggerFactory.getLogger(NuxeoClientEmbedded.class);
\r
68 private LoginHandler loginHandler;
\r
70 private final HashMap<String, RepositoryInstance> repositoryInstances;
\r
72 private final ListenerList connectionListeners;
\r
74 private InvokerLocator locator;
\r
76 private String serverName;
\r
78 private final AutoConfigurationService cfg;
\r
80 private RepositoryManager repositoryMgr;
\r
82 private static final NuxeoClientEmbedded instance = new NuxeoClientEmbedded();
\r
84 private static final Log log = LogFactory.getLog(NuxeoClientEmbedded.class);
\r
87 * Constructs a new NuxeoClient. NOTE: Using {@link #getInstance()} instead
\r
88 * of this constructor is recommended.
\r
90 public NuxeoClientEmbedded() {
\r
91 connectionListeners = new ListenerList();
\r
92 cfg = new AutoConfigurationService();
\r
93 loginHandler = loginHandler == null ? new DefaultLoginHandler()
\r
95 repositoryInstances = new HashMap<String, RepositoryInstance>();
\r
98 public static NuxeoClientEmbedded getInstance() {
\r
103 private synchronized void disconnect() throws Exception {
\r
104 if (locator == null) {
\r
105 throw new IllegalStateException("Client is not connected");
\r
110 public synchronized void tryDisconnect() throws Exception {
\r
111 if (locator == null) {
\r
112 return; // do nothing
\r
117 private void doDisconnect() throws Exception {
\r
120 // close repository sessions if any
\r
121 Iterator<Entry<String, RepositoryInstance>> it = repositoryInstances.entrySet().iterator();
\r
122 while (it.hasNext()) {
\r
123 Entry<String, RepositoryInstance> repo = it.next();
\r
125 repo.getValue().close();
\r
126 } catch (Exception e) {
\r
127 log.debug("Error while trying to close " + repo, e);
\r
132 repositoryMgr = null;
\r
135 public synchronized String getServerName() {
\r
136 if (locator == null) {
\r
137 throw new IllegalStateException("Client is not connected");
\r
139 if (serverName == null) {
\r
140 if (cfg == null) { // compatibility
\r
141 serverName = RemotingService.ping(locator.getHost(),
\r
142 locator.getPort());
\r
144 serverName = cfg.getServerConfiguration().getProductInfo();
\r
150 public synchronized boolean isConnected() {
\r
154 public InvokerLocator getLocator() {
\r
158 public synchronized LoginHandler getLoginHandler() {
\r
159 return loginHandler;
\r
162 public synchronized void setLoginHandler(LoginHandler loginHandler) {
\r
163 this.loginHandler = loginHandler;
\r
166 public RepositoryManager getRepositoryManager() throws Exception {
\r
167 if (repositoryMgr == null) {
\r
168 repositoryMgr = Framework.getService(RepositoryManager.class);
\r
170 return repositoryMgr;
\r
174 * Gets the repositories available on the connected server.
\r
176 * @return the repositories
\r
178 public Repository[] getRepositories() throws Exception {
\r
179 Collection<Repository> repos = getRepositoryManager().getRepositories();
\r
180 return repos.toArray(new Repository[repos.size()]);
\r
183 public Repository getDefaultRepository() throws Exception {
\r
184 return getRepositoryManager().getDefaultRepository();
\r
187 public Repository getRepository(String name) throws Exception {
\r
188 return getRepositoryManager().getRepository(name);
\r
192 * Open a Nuxeo repo session using the passed in repoDomain and use the default tx timeout period
\r
194 public RepositoryInstance openRepository(RepositoryDomainType repoDomain) throws Exception {
\r
195 return openRepository(repoDomain.getRepositoryName(), -1);
\r
199 * Open a Nuxeo repo session using the passed in repoDomain and use the default tx timeout period
\r
201 public RepositoryInstance openRepository(String repoName) throws Exception {
\r
202 return openRepository(repoName, -1);
\r
206 * Open a Nuxeo repo session using the default repo with the specified (passed in) tx timeout period
\r
209 public RepositoryInstance openRepository(int timeoutSeconds) throws Exception {
\r
210 return openRepository(null, timeoutSeconds);
\r
214 * Open a Nuxeo repo session using the default repo with the default tx timeout period
\r
217 public RepositoryInstance openRepository() throws Exception {
\r
218 return openRepository(null, -1 /*default timeout period*/);
\r
221 public RepositoryInstance openRepository(String repoName, int timeoutSeconds) throws Exception {
\r
222 RepositoryInstance result = null;
\r
224 if (timeoutSeconds > 0) {
\r
225 TransactionManager transactionMgr = TransactionHelper.lookupTransactionManager();
\r
226 transactionMgr.setTransactionTimeout(timeoutSeconds);
\r
227 if (logger.isInfoEnabled()) {
\r
228 logger.info(String.format("Changing current request's transaction timeout period to %d seconds",
\r
233 // REM 10/29/2013 - We may want to add this clause if (!TransactionHelper.isTransactionActive()) {
\r
234 boolean startTransaction = TransactionHelper.startTransaction();
\r
235 if (startTransaction == false) {
\r
236 logger.warn("Could not start a Nuxeo transaction with the TransactionHelper class.");
\r
239 Repository repository = null;
\r
240 if (repoName != null) {
\r
241 repository = getRepositoryManager().getRepository(repoName);
\r
243 repository = getRepositoryManager().getDefaultRepository(); // Add a log info statement here stating that since no repo name was given we'll use the default repo instead
\r
246 if (repository != null) {
\r
247 result = newRepositoryInstance(repository);
\r
248 String key = result.getSessionId();
\r
249 repositoryInstances.put(key, result);
\r
250 if (logger.isTraceEnabled()) {
\r
251 logger.trace("Added a new repository instance to our repo list. Current count is now: "
\r
252 + repositoryInstances.size());
\r
255 String errMsg = String.format("Could not open a session to the Nuxeo repository='%s'", repoName);
\r
256 logger.error(errMsg);
\r
257 throw new Exception(errMsg);
\r
263 public void releaseRepository(RepositoryInstance repo) throws Exception {
\r
264 String key = repo.getSessionId();
\r
269 } catch (Exception e) {
\r
270 logger.error("Possible data loss. Could not save and/or release the repository.", e);
\r
273 RepositoryInstance wasRemoved = repositoryInstances.remove(key);
\r
274 if (logger.isTraceEnabled()) {
\r
275 if (wasRemoved != null) {
\r
276 logger.trace("Removed a repository instance from our repo list. Current count is now: "
\r
277 + repositoryInstances.size());
\r
279 logger.trace("Could not remove a repository instance from our repo list. Current count is now: "
\r
280 + repositoryInstances.size());
\r
283 //if (TransactionHelper.isTransactionActiveOrMarkedRollback())
\r
284 TransactionHelper.commitOrRollbackTransaction();
\r
288 public static RepositoryInstance newRepositoryInstance(Repository repository) {
\r
289 return new RepositoryInstanceHandler(repository).getProxy(); // Why a proxy here?
\r