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 * bstefanescu 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.core.repository; 023 024import org.nuxeo.ecm.core.api.CoreSession; 025 026/** 027 * A repository initialization handler is responsible for initializing a repository content. 028 * <p> 029 * The handler is called each time a repository is opened in a JVM session. This can be used to create a default 030 * structure for the repository. 031 * <p> 032 * To register a repository initializer <code>MyInitHandler</code> you should do: 033 * <p> 034 * <code>RepositoryInitializationHandler.setInstance(new MyInitHandler());</code> 035 * <p> 036 * If you want to create an initialization chain you can implement to delegate to the parent handle the default 037 * initialization and then to do your specific initialization stuff 038 * 039 * <pre> 040 * <code> 041 * RepositoryInitializationHandler parentHandler = RepositoryInitializationHandler.getInstance(); 042 * MyInitHandler myHandler = new MyInitHandler(parentHandler); 043 * RepositoryInitializationHandler.setInstance(myHandler); 044 * ... 045 * class MyHandler extends RepositoryInitializationHandler { 046 * ... 047 * public initializeRepository(CoreSession session) { 048 * if (parentHandler != null) parentHandler.initializeRepository(session); 049 * // do my own initialization here 050 * ... 051 * } 052 * ... 053 * } 054 * </code> 055 * </pre> 056 * <p> 057 * <b>Important Note:</b> Use the given session to initialize the repository. Do not create other repository sessions 058 * when initializing the repository to avoid dead locks. 059 * 060 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 061 */ 062public abstract class RepositoryInitializationHandler { 063 064 protected static RepositoryInitializationHandler instance; 065 066 public static RepositoryInitializationHandler getInstance() { 067 return instance; 068 } 069 070 /** 071 * The parent handler if any otherwise null 072 */ 073 protected RepositoryInitializationHandler previous; 074 075 /** 076 * The next handler in the chain if any or null otherwise 077 */ 078 protected RepositoryInitializationHandler next; 079 080 public abstract void doInitializeRepository(CoreSession session); 081 082 /** 083 * Must be implemented by custom initializers. 084 * 085 * @param session the current session 086 */ 087 public void initializeRepository(CoreSession session) { 088 synchronized (RepositoryInitializationHandler.class) { 089 if (previous != null) { 090 previous.initializeRepository(session); 091 } 092 doInitializeRepository(session); 093 } 094 } 095 096 public void install() { 097 synchronized (RepositoryInitializationHandler.class) { 098 previous = instance; 099 if (previous != null) { 100 previous.next = this; 101 } 102 instance = this; 103 } 104 } 105 106 public void uninstall() { 107 synchronized (RepositoryInitializationHandler.class) { 108 if (previous != null) { 109 previous.next = next; 110 } 111 if (next != null) { 112 next.previous = previous; 113 } 114 if (instance == this) { 115 instance = previous; 116 } 117 } 118 } 119 120 public RepositoryInitializationHandler getPrevious() { 121 return previous; 122 } 123 124 public RepositoryInitializationHandler getNext() { 125 return next; 126 } 127 128}