001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 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 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021package org.nuxeo.ecm.core.management.storage; 022 023import org.nuxeo.ecm.core.api.CoreSession; 024import org.nuxeo.ecm.core.api.NuxeoException; 025import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner; 026import org.nuxeo.ecm.core.management.api.StorageError; 027import org.nuxeo.runtime.api.Framework; 028 029/** 030 * Runner dedicated to mgmt doc operations 031 * 032 * @author "Stephane Lacoin [aka matic] <slacoin at nuxeo.com>" 033 */ 034public abstract class DocumentStoreSessionRunner extends UnrestrictedSessionRunner { 035 036 protected static String repositoryName; 037 038 /** 039 * Should be Invoked at startup 040 */ 041 public static void setRepositoryName(String name) { 042 repositoryName = name; 043 } 044 045 public DocumentStoreSessionRunner() { 046 super(repositoryName); 047 } 048 049 public DocumentStoreSessionRunner(CoreSession session) { 050 super(session); 051 if (!repositoryName.equals(session.getRepositoryName())) { 052 throw new IllegalArgumentException("Session is not attached to " + repositoryName); 053 } 054 } 055 056 /** 057 * Run with the nuxeo class loader, wrap client exception into errors 058 */ 059 public void runSafe() { 060 ClassLoader jarCL = Thread.currentThread().getContextClassLoader(); 061 ClassLoader bundleCL = Framework.class.getClassLoader(); 062 try { 063 Thread.currentThread().setContextClassLoader(bundleCL); 064 runUnrestricted(); 065 } catch (NuxeoException e) { 066 throw new StorageError("Storage error : " + errorMessage(), e); 067 } finally { 068 Thread.currentThread().setContextClassLoader(jarCL); 069 } 070 } 071 072 protected String errorMessage() { 073 return String.format("%s:%s", getClass().getCanonicalName(), this.toString()); 074 } 075 076}