jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r161889 - in incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core: ItemManager.java RepositoryImpl.java nodetype/NodeTypeDefStore.java nodetype/NodeTypeRegistry.java
Date Tue, 19 Apr 2005 14:01:46 GMT
Author: stefan
Date: Tue Apr 19 07:01:45 2005
New Revision: 161889

URL: http://svn.apache.org/viewcvs?view=rev&rev=161889
Log:
added some low-level hooks to NodeTypeRegistry & RepositoryImpl to improve re-usability
of certain components

Modified:
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/ItemManager.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/RepositoryImpl.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/NodeTypeDefStore.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/NodeTypeRegistry.java

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/ItemManager.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/ItemManager.java?view=diff&r1=161888&r2=161889
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/ItemManager.java (original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/ItemManager.java Tue Apr
19 07:01:45 2005
@@ -242,7 +242,7 @@
      * @param path path to the item to be checked
      * @return true if the specified item exists
      */
-    boolean itemExists(Path path) {
+    public boolean itemExists(Path path) {
         try {
             // check sanity of session
             session.sanityCheck();
@@ -279,7 +279,7 @@
      * @param id id of the item to be checked
      * @return true if the specified item exists
      */
-    boolean itemExists(ItemId id) {
+    public boolean itemExists(ItemId id) {
         try {
             // check sanity of session
             session.sanityCheck();
@@ -321,7 +321,7 @@
      * @throws AccessDeniedException
      * @throws RepositoryException
      */
-    synchronized ItemImpl getItem(Path path)
+    public synchronized ItemImpl getItem(Path path)
             throws PathNotFoundException, AccessDeniedException, RepositoryException {
         ItemId id = hierMgr.resolvePath(path);
         try {

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/RepositoryImpl.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/RepositoryImpl.java?view=diff&r1=161888&r2=161889
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/RepositoryImpl.java (original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/RepositoryImpl.java Tue
Apr 19 07:01:45 2005
@@ -30,8 +30,8 @@
 import org.apache.jackrabbit.core.nodetype.NodeTypeImpl;
 import org.apache.jackrabbit.core.nodetype.NodeTypeRegistry;
 import org.apache.jackrabbit.core.nodetype.virtual.VirtualNodeTypeStateManager;
-import org.apache.jackrabbit.core.observation.ObservationManagerFactory;
 import org.apache.jackrabbit.core.observation.DelegatingObservationDispatcher;
+import org.apache.jackrabbit.core.observation.ObservationManagerFactory;
 import org.apache.jackrabbit.core.security.CredentialsCallbackHandler;
 import org.apache.jackrabbit.core.state.ItemStateException;
 import org.apache.jackrabbit.core.state.PMContext;
@@ -63,12 +63,12 @@
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
+import java.security.AccessControlContext;
+import java.security.AccessController;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Properties;
-import java.security.AccessControlContext;
-import java.security.AccessController;
 
 /**
  * A <code>RepositoryImpl</code> ...
@@ -108,7 +108,7 @@
     private final VirtualNodeTypeStateManager virtNTMgr;
 
     // configuration of the repository
-    private final RepositoryConfig repConfig;
+    protected final RepositoryConfig repConfig;
 
     // the master filesystem
     private final FileSystem repStore;
@@ -116,7 +116,9 @@
     // sub file system where the repository stores meta data such as uuid of root node, etc.
     private final FileSystem metaDataStore;
 
-    /** the delegating observation dispatcher for all workspaces */
+    /**
+     * the delegating observation dispatcher for all workspaces
+     */
     private final DelegatingObservationDispatcher delegatingDispatcher =
             new DelegatingObservationDispatcher();
 
@@ -143,7 +145,7 @@
      *
      * @param repConfig
      */
-    private RepositoryImpl(RepositoryConfig repConfig) throws RepositoryException {
+    protected RepositoryImpl(RepositoryConfig repConfig) throws RepositoryException {
         this.repConfig = repConfig;
 
         // setup file systems
@@ -263,9 +265,9 @@
             wspInfos.put(config.getName(), info);
         }
 
-        nsReg = new NamespaceRegistryImpl(new BasedFileSystem(repStore, "/namespaces"));
+        nsReg = createNamespaceRegistry(new BasedFileSystem(repStore, "/namespaces"));
 
-        ntReg = NodeTypeRegistry.create(nsReg, new BasedFileSystem(repStore, "/nodetypes"));
+        ntReg = createNodeTypeRegistry(nsReg, new BasedFileSystem(repStore, "/nodetypes"));
 
         // init version manager
         VersioningConfig vConfig = repConfig.getVersioningConfig();
@@ -303,6 +305,31 @@
     }
 
     /**
+     * Creates the <code>NamespaceRegistry</code> instance.
+     *
+     * @param fs
+     * @return
+     * @throws RepositoryException
+     */
+    protected NamespaceRegistryImpl createNamespaceRegistry(FileSystem fs)
+            throws RepositoryException {
+        return new NamespaceRegistryImpl(fs);
+    }
+
+    /**
+     * Creates the <code>NodeTypeRegistry</code> instance.
+     *
+     * @param fs
+     * @return
+     * @throws RepositoryException
+     */
+    protected NodeTypeRegistry createNodeTypeRegistry(NamespaceRegistry nsReg,
+                                                      FileSystem fs)
+            throws RepositoryException {
+        return NodeTypeRegistry.create(nsReg, fs);
+    }
+
+    /**
      * Creates a new <code>RepositoryImpl</code> instance.
      * <p/>
      * todo prevent multiple instantiation from same configuration as this could lead to
data corruption/loss
@@ -316,6 +343,20 @@
         return new RepositoryImpl(config);
     }
 
+    /**
+     * Performs a sanity check on this repository instance.
+     *
+     * @throws IllegalStateException if this repository has been rendered
+     *                               invalid for some reason (e.g. if it has
+     *                               been shut down)
+     */
+    protected void sanityCheck() throws IllegalStateException {
+        // check repository status
+        if (disposed) {
+            throw new IllegalStateException("repository instance has been shut down");
+        }
+    }
+
     private void initWorkspace(String wspName) throws RepositoryException {
         // get system session and Workspace instance
         SessionImpl sysSession = getSystemSession(wspName);
@@ -367,46 +408,36 @@
     }
 
     RepositoryConfig getConfig() {
-        // check state
-        if (disposed) {
-            throw new IllegalStateException("repository instance has been shut down");
-        }
+        // check sanity of this instance
+        sanityCheck();
 
         return repConfig;
     }
 
     NamespaceRegistryImpl getNamespaceRegistry() {
-        // check state
-        if (disposed) {
-            throw new IllegalStateException("repository instance has been shut down");
-        }
+        // check sanity of this instance
+        sanityCheck();
 
         return nsReg;
     }
 
     NodeTypeRegistry getNodeTypeRegistry() {
-        // check state
-        if (disposed) {
-            throw new IllegalStateException("repository instance has been shut down");
-        }
+        // check sanity of this instance
+        sanityCheck();
 
         return ntReg;
     }
 
     VersionManager getVersionManager() {
-        // check state
-        if (disposed) {
-            throw new IllegalStateException("repository instance has been shut down");
-        }
+        // check sanity of this instance
+        sanityCheck();
 
         return vMgr;
     }
 
     String getRootNodeUUID() {
-        // check state
-        if (disposed) {
-            throw new IllegalStateException("repository instance has been shut down");
-        }
+        // check sanity of this instance
+        sanityCheck();
 
         return rootNodeUUID;
     }
@@ -422,6 +453,30 @@
     }
 
     /**
+     * Returns the {@link WorkspaceInfo} for the named workspace.
+     *
+     * @param workspaceName The name of the workspace whose {@link WorkspaceInfo}
+     *                      is to be returned. This must not be <code>null</code>.
+     * @return The {@link WorkspaceInfo} for the named workspace. This will
+     *         never be <code>null</code>.
+     * @throws IllegalStateException    If this repository has already been
+     *                                  shut down.
+     * @throws NoSuchWorkspaceException If the named workspace does not exist.
+     */
+    protected WorkspaceInfo getWorkspaceInfo(String workspaceName)
+            throws NoSuchWorkspaceException {
+        // check sanity of this instance
+        sanityCheck();
+
+        WorkspaceInfo wspInfo = (WorkspaceInfo) wspInfos.get(workspaceName);
+        if (wspInfo == null) {
+            throw new NoSuchWorkspaceException(workspaceName);
+        }
+
+        return wspInfo;
+    }
+
+    /**
      * Creates a workspace with the given name.
      *
      * @param workspaceName name of the new workspace
@@ -445,30 +500,18 @@
 
     SharedItemStateManager getWorkspaceStateManager(String workspaceName)
             throws NoSuchWorkspaceException, RepositoryException {
-        // check state
-        if (disposed) {
-            throw new IllegalStateException("repository instance has been shut down");
-        }
+        // check sanity of this instance
+        sanityCheck();
 
-        WorkspaceInfo wspInfo = (WorkspaceInfo) wspInfos.get(workspaceName);
-        if (wspInfo == null) {
-            throw new NoSuchWorkspaceException(workspaceName);
-        }
-        return wspInfo.getItemStateProvider();
+        return getWorkspaceInfo(workspaceName).getItemStateProvider();
     }
 
     ObservationManagerFactory getObservationManagerFactory(String workspaceName)
             throws NoSuchWorkspaceException {
-        // check state
-        if (disposed) {
-            throw new IllegalStateException("repository instance has been shut down");
-        }
+        // check sanity of this instance
+        sanityCheck();
 
-        WorkspaceInfo wspInfo = (WorkspaceInfo) wspInfos.get(workspaceName);
-        if (wspInfo == null) {
-            throw new NoSuchWorkspaceException(workspaceName);
-        }
-        return wspInfo.getObservationManagerFactory();
+        return getWorkspaceInfo(workspaceName).getObservationManagerFactory();
     }
 
     /**
@@ -486,16 +529,10 @@
      */
     SearchManager getSearchManager(String workspaceName)
             throws NoSuchWorkspaceException, RepositoryException {
-        // check state
-        if (disposed) {
-            throw new IllegalStateException("repository instance has been shut down");
-        }
+        // check sanity of this instance
+        sanityCheck();
 
-        WorkspaceInfo wspInfo = (WorkspaceInfo) wspInfos.get(workspaceName);
-        if (wspInfo == null) {
-            throw new NoSuchWorkspaceException(workspaceName);
-        }
-        return wspInfo.getSearchManager();
+        return getWorkspaceInfo(workspaceName).getSearchManager();
     }
 
     /**
@@ -509,16 +546,10 @@
      */
     LockManager getLockManager(String workspaceName) throws
             NoSuchWorkspaceException, RepositoryException {
+        // check sanity of this instance
+        sanityCheck();
 
-        if (disposed) {
-            throw new IllegalStateException("repository instance has been shut down");
-        }
-
-        WorkspaceInfo wspInfo = (WorkspaceInfo) wspInfos.get(workspaceName);
-        if (wspInfo == null) {
-            throw new NoSuchWorkspaceException(workspaceName);
-        }
-        return wspInfo.getLockManager();
+        return getWorkspaceInfo(workspaceName).getLockManager();
     }
 
     /**
@@ -529,19 +560,12 @@
      */
     SystemSession getSystemSession(String workspaceName)
             throws NoSuchWorkspaceException, RepositoryException {
-        // check state
-        if (disposed) {
-            throw new IllegalStateException("repository instance has been shut down");
-        }
+        // check sanity of this instance
+        sanityCheck();
 
-        WorkspaceInfo wspInfo = (WorkspaceInfo) wspInfos.get(workspaceName);
-        if (wspInfo == null) {
-            throw new NoSuchWorkspaceException(workspaceName);
-        }
-        return wspInfo.getSystemSession();
+        return getWorkspaceInfo(workspaceName).getSystemSession();
     }
 
-
     /**
      * Creates a new session on the specified workspace for the
      * authenticated subject of the given login context.
@@ -555,14 +579,12 @@
      *                                  workspace
      * @throws RepositoryException      if another error occurs
      */
-    SessionImpl createSession(LoginContext loginContext, String workspaceName)
+    SessionImpl createSession(LoginContext loginContext,
+                              String workspaceName)
             throws NoSuchWorkspaceException, AccessDeniedException,
             RepositoryException {
-        WorkspaceInfo wspInfo = (WorkspaceInfo) wspInfos.get(workspaceName);
-        if (wspInfo == null) {
-            throw new NoSuchWorkspaceException(workspaceName);
-        }
-        XASessionImpl ses = new XASessionImpl(this, loginContext, wspInfo.getConfig());
+        WorkspaceInfo wspInfo = getWorkspaceInfo(workspaceName);
+        SessionImpl ses = createSessionInstance(loginContext, wspInfo.getConfig());
         activeSessions.put(ses, ses);
         return ses;
     }
@@ -583,11 +605,8 @@
     SessionImpl createSession(Subject subject, String workspaceName)
             throws NoSuchWorkspaceException, AccessDeniedException,
             RepositoryException {
-        WorkspaceInfo wspInfo = (WorkspaceInfo) wspInfos.get(workspaceName);
-        if (wspInfo == null) {
-            throw new NoSuchWorkspaceException(workspaceName);
-        }
-        XASessionImpl ses = new XASessionImpl(this, subject, wspInfo.getConfig());
+        WorkspaceInfo wspInfo = getWorkspaceInfo(workspaceName);
+        SessionImpl ses = createSessionInstance(subject, wspInfo.getConfig());
         activeSessions.put(ses, ses);
         return ses;
     }
@@ -599,7 +618,7 @@
      * @see Runtime#addShutdownHook(Thread)
      */
     public synchronized void shutdown() {
-        // check state
+        // check status of this instance
         if (disposed) {
             // there's nothing to do here because the repository has already been shut down
             return;
@@ -658,15 +677,38 @@
         disposed = true;
     }
 
+    /**
+     * Returns an <code>InputStream</code> on a <code>Properties</code>
resource
+     * which contains the default properties for the repository. This method is
+     * only called once during repository initialization.
+     * <p/>
+     * The <code>InputStream</code> returned is closed by the caller.
+     * <p/>
+     * This method returns an <code>InputStream</code> on the
+     * <code>org/apache/jackrabbit/core/repository.properties</code> resource
+     * found in the class path.
+     *
+     * @return <code>InputStream</code> on a <code>Properties</code>
resource
+     *         or <code>null</code> if the resource does not exist.
+     */
+    protected InputStream getDefaultRepositoryProperties() {
+        return RepositoryImpl.class.getResourceAsStream("repository.properties");
+    }
+
     private void loadRepProps() throws RepositoryException {
         FileSystemResource propFile = new FileSystemResource(metaDataStore, PROPERTIES_RESOURCE);
         try {
             repProps.clear();
             if (!propFile.exists() || propFile.length() == 0) {
                 // initialize properties with pre-defined values
-                InputStream in = RepositoryImpl.class.getResourceAsStream("repository.properties");
-                repProps.load(in);
-                in.close();
+                InputStream in = getDefaultRepositoryProperties();
+                if (in != null) {
+                    try {
+                        repProps.load(in);
+                    } finally {
+                        in.close();
+                    }
+                }
 
                 // set counts
                 repProps.setProperty(STATS_NODE_COUNT_PROPERTY, Long.toString(nodesCount));
@@ -719,9 +761,8 @@
      * @throws RepositoryException if the persistence manager could
      *                             not be instantiated/initialized
      */
-    private static PersistenceManager createPersistenceManager(
-            File homeDir, FileSystem fs, PersistenceManagerConfig pmConfig,
-            String rootNodeUUID, NamespaceRegistry nsReg, NodeTypeRegistry ntReg)
+    private static PersistenceManager createPersistenceManager(File homeDir, FileSystem fs,
PersistenceManagerConfig pmConfig,
+                                                               String rootNodeUUID, NamespaceRegistry
nsReg, NodeTypeRegistry ntReg)
             throws RepositoryException {
         try {
             PersistenceManager pm = (PersistenceManager) pmConfig.newInstance();
@@ -741,18 +782,15 @@
      */
     public Session login(Credentials credentials, String workspaceName)
             throws LoginException, NoSuchWorkspaceException, RepositoryException {
-        // check state
-        if (disposed) {
-            throw new IllegalStateException("repository instance has been shut down");
-        }
+        // check sanity of this instance
+        sanityCheck();
 
         if (workspaceName == null) {
             workspaceName = repConfig.getDefaultWorkspaceName();
         }
 
-        if (!wspInfos.containsKey(workspaceName)) {
-            throw new NoSuchWorkspaceException(workspaceName);
-        }
+        // check if workspace exists (will throw NoSuchWorkspaceException if not)
+        getWorkspaceInfo(workspaceName);
 
         if (credentials == null) {
             // null credentials, obtain the identity of the already-authenticated
@@ -855,7 +893,7 @@
      * {@inheritDoc}
      */
     public void onEvent(EventIterator events) {
-        // check state
+        // check status of this instance
         if (disposed) {
             // ignore, repository instance has been shut down
             return;
@@ -885,6 +923,39 @@
         }
     }
 
+    //-----------------------------------------< overrideable factory methods >
+    /**
+     * Creates an instance of the {@link SessionImpl} class representing a
+     * user authenticated by the <code>loginContext</code> instance attached
+     * to the workspace configured by the <code>wspConfig</code>.
+     *
+     * @throws AccessDeniedException when ??
+     * @throws RepositoryException   If any other error occurrs creating the
+     *                               session.
+     */
+    protected SessionImpl createSessionInstance(LoginContext loginContext,
+                                                WorkspaceConfig wspConfig)
+            throws AccessDeniedException, RepositoryException {
+
+        return new XASessionImpl(this, loginContext, wspConfig);
+    }
+
+    /**
+     * Creates an instance of the {@link SessionImpl} class representing a
+     * user represented by the <code>subject</code> instance attached
+     * to the workspace configured by the <code>wspConfig</code>.
+     *
+     * @throws AccessDeniedException when ??
+     * @throws RepositoryException   If any other error occurrs creating the
+     *                               session.
+     */
+    protected SessionImpl createSessionInstance(Subject subject,
+                                                WorkspaceConfig wspConfig)
+            throws AccessDeniedException, RepositoryException {
+
+        return new XASessionImpl(this, subject, wspConfig);
+    }
+
     //--------------------------------------------------------< inner classes >
     /**
      * <code>WorkspaceInfo</code> holds the objects that are shared
@@ -1041,7 +1112,7 @@
          * Returns the search manager for this workspace
          *
          * @return the search manager for this workspace, or <code>null</code>
-         *  if no <code>SearchManager</code>
+         *         if no <code>SearchManager</code>
          * @throws RepositoryException if the search manager could not be created
          */
         synchronized SearchManager getSearchManager() throws RepositoryException {

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/NodeTypeDefStore.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/NodeTypeDefStore.java?view=diff&r1=161888&r2=161889
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/NodeTypeDefStore.java
(original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/NodeTypeDefStore.java
Tue Apr 19 07:01:45 2005
@@ -33,7 +33,7 @@
 /**
  * <code>NodeTypeDefStore</code> ...
  */
-class NodeTypeDefStore {
+public class NodeTypeDefStore {
 
     /** Map of node type names to node type definitions. */
     private final HashMap ntDefs;
@@ -41,7 +41,7 @@
     /**
      * Empty default constructor.
      */
-    NodeTypeDefStore() throws RepositoryException {
+    public NodeTypeDefStore() throws RepositoryException {
         ntDefs = new HashMap();
     }
 
@@ -50,7 +50,7 @@
      * @throws IOException
      * @throws InvalidNodeTypeDefException
      */
-    void load(InputStream in)
+    public void load(InputStream in)
             throws IOException, InvalidNodeTypeDefException,
             RepositoryException {
         NodeTypeDef[] types = NodeTypeReader.read(in);
@@ -65,7 +65,7 @@
      * @throws IOException
      * @throws RepositoryException
      */
-    void store(OutputStream out, NamespaceRegistry registry)
+    public void store(OutputStream out, NamespaceRegistry registry)
             throws IOException, RepositoryException {
         NodeTypeDef[] types = (NodeTypeDef[])
             ntDefs.values().toArray(new NodeTypeDef[ntDefs.size()]);
@@ -75,7 +75,7 @@
     /**
      * @param ntd
      */
-    void add(NodeTypeDef ntd) {
+    public void add(NodeTypeDef ntd) {
         ntDefs.put(ntd.getName(), ntd);
     }
 
@@ -83,14 +83,14 @@
      * @param name
      * @return
      */
-    boolean remove(QName name) {
+    public boolean remove(QName name) {
         return ntDefs.remove(name) != null ? true : false;
     }
 
     /**
      *
      */
-    void removeAll() {
+    public void removeAll() {
         ntDefs.clear();
     }
 
@@ -98,7 +98,7 @@
      * @param name
      * @return
      */
-    boolean contains(QName name) {
+    public boolean contains(QName name) {
         return ntDefs.containsKey(name);
     }
 
@@ -106,14 +106,14 @@
      * @param name
      * @return
      */
-    NodeTypeDef get(QName name) {
+    public NodeTypeDef get(QName name) {
         return (NodeTypeDef) ntDefs.get(name);
     }
 
     /**
      * @return
      */
-    Collection all() {
+    public Collection all() {
         return Collections.unmodifiableCollection(ntDefs.values());
     }
 }

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/NodeTypeRegistry.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/NodeTypeRegistry.java?view=diff&r1=161888&r2=161889
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/NodeTypeRegistry.java
(original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/NodeTypeRegistry.java
Tue Apr 19 07:01:45 2005
@@ -51,11 +51,13 @@
  * A <code>NodeTypeRegistry</code> ...
  */
 public class NodeTypeRegistry implements Constants {
+
     private static Logger log = Logger.getLogger(NodeTypeRegistry.class);
 
     private static final String BUILTIN_NODETYPES_RESOURCE_PATH =
             "org/apache/jackrabbit/core/nodetype/builtin_nodetypes.xml";
-    private static final String CUSTOM_NODETYPES_RESOURCE_NAME = "custom_nodetypes.xml";
+    private static final String CUSTOM_NODETYPES_RESOURCE_NAME =
+            "custom_nodetypes.xml";
 
     // file system where node type registrations are persisted
     private final FileSystem ntStore;
@@ -122,13 +124,13 @@
     }
 
     /**
-     * Private constructor
+     * Protected constructor
      *
      * @param nsReg
      * @param ntStore
      * @throws RepositoryException
      */
-    private NodeTypeRegistry(NamespaceRegistry nsReg, FileSystem ntStore)
+    protected NodeTypeRegistry(NamespaceRegistry nsReg, FileSystem ntStore)
             throws RepositoryException {
         this.nsReg = nsReg;
         this.ntStore = ntStore;
@@ -157,23 +159,17 @@
 
         // load and register pre-defined (i.e. built-in) node types
         /**
-         * temporarily disable checking that auto-create properties have
+         * temporarily disable checking that auto-created properties have
          * default values
          */
         checkAutoCreatePropHasDefault = false;
         builtInNTDefs = new NodeTypeDefStore();
-        InputStream in = null;
         try {
-            in = getClass().getClassLoader().getResourceAsStream(
-                    BUILTIN_NODETYPES_RESOURCE_PATH);
-            builtInNTDefs.load(in);
+            // load built-in node type definitions
+            loadBuiltInNodeTypeDefs(builtInNTDefs);
+            
+            // validate & register built-in node types
             internalRegister(builtInNTDefs.all());
-        } catch (IOException ioe) {
-            String error =
-                    "internal error: failed to read built-in node type definitions stored
in "
-                    + BUILTIN_NODETYPES_RESOURCE_PATH;
-            log.debug(error);
-            throw new RepositoryException(error, ioe);
         } catch (InvalidNodeTypeDefException intde) {
             String error =
                     "internal error: invalid built-in node type definition stored in "
@@ -182,57 +178,26 @@
             throw new RepositoryException(error, intde);
         } finally {
             /**
-             * re-enable checking that auto-create properties have default values
+             * re-enable checking that auto-created properties have default values
              */
             checkAutoCreatePropHasDefault = true;
-            if (in != null) {
-                try {
-                    in.close();
-                } catch (IOException ioe) {
-                    // ignore
-                }
-            }
         }
 
         // load and register custom node types
         customNTDefs = new NodeTypeDefStore();
-        in = null;
+
+        // load custom node type definitions
+        loadCustomNodeTypeDefs(customNTDefs);
+
+        // validate & register custom node types
         try {
-            if (customNodeTypesResource.exists()) {
-                in = customNodeTypesResource.getInputStream();
-            }
-        } catch (FileSystemException fse) {
+            internalRegister(customNTDefs.all());
+        } catch (InvalidNodeTypeDefException intde) {
             String error =
-                    "internal error: failed to access custom node type definitions stored
in "
+                    "internal error: invalid custom node type definition stored in "
                     + customNodeTypesResource.getPath();
             log.debug(error);
-            throw new RepositoryException(error, fse);
-        }
-        if (in == null) {
-            log.info("no custom node type definitions found");
-        } else {
-            try {
-                customNTDefs.load(in);
-                internalRegister(customNTDefs.all());
-            } catch (IOException ioe) {
-                String error =
-                        "internal error: failed to read custom node type definitions stored
in "
-                        + customNodeTypesResource.getPath();
-                log.debug(error);
-                throw new RepositoryException(error, ioe);
-            } catch (InvalidNodeTypeDefException intde) {
-                String error =
-                        "internal error: invalid custom node type definition stored in "
-                        + customNodeTypesResource.getPath();
-                log.debug(error);
-                throw new RepositoryException(error, intde);
-            } finally {
-                try {
-                    in.close();
-                } catch (IOException ioe) {
-                    // ignore
-                }
-            }
+            throw new RepositoryException(error, intde);
         }
     }
 
@@ -256,7 +221,7 @@
      * objects. An <code>InvalidNodeTypeDefException</code> is thrown if the
      * validation of any of the contained <code>NodeTypeDef</code> objects fails.
      * <p/>
-     * Note that in the case an exception is thrown, some node types might have
+     * Note that in the case an exception is thrown some node types might have
      * been nevertheless successfully registered.
      *
      * @param ntDefs collection of <code>NodeTypeDef</code> objects
@@ -379,34 +344,6 @@
         }
     }
 
-    private void persistCustomNTDefs() throws RepositoryException {
-        OutputStream out = null;
-        try {
-            out = customNodeTypesResource.getOutputStream();
-            customNTDefs.store(out, nsReg);
-        } catch (IOException ioe) {
-            String error =
-                    "internal error: failed to persist custom node type definitions to "
-                    + customNodeTypesResource.getPath();
-            log.debug(error);
-            throw new RepositoryException(error, ioe);
-        } catch (FileSystemException fse) {
-            String error =
-                    "internal error: failed to persist custom node type definitions to "
-                    + customNodeTypesResource.getPath();
-            log.debug(error);
-            throw new RepositoryException(error, fse);
-        } finally {
-            if (out != null) {
-                try {
-                    out.close();
-                } catch (IOException ioe) {
-                    // ignore
-                }
-            }
-        }
-    }
-
     /**
      * Add a <code>NodeTypeRegistryListener</code>
      *
@@ -1132,7 +1069,7 @@
 
         // persist new node type definition
         customNTDefs.add(ntd);
-        persistCustomNTDefs();
+        persistCustomNodeTypeDefs(customNTDefs);
 
         // notify listeners
         notifyRegistered(ntd.getName());
@@ -1189,7 +1126,7 @@
                 QName ntName = (QName) iter.next();
                 customNTDefs.add((NodeTypeDef) registeredNTDefs.get(ntName));
             }
-            persistCustomNTDefs();
+            persistCustomNodeTypeDefs(customNTDefs);
 
             // notify listeners
             for (Iterator iter = newNTNames.iterator(); iter.hasNext();) {
@@ -1207,17 +1144,17 @@
     }
 
     /**
-     * @param name
+     * @param nodeTypeName
      * @throws NoSuchNodeTypeException
      * @throws RepositoryException
      */
-    public synchronized void unregisterNodeType(QName name)
+    public synchronized void unregisterNodeType(QName nodeTypeName)
             throws NoSuchNodeTypeException, RepositoryException {
-        if (!registeredNTDefs.containsKey(name)) {
-            throw new NoSuchNodeTypeException(name.toString());
+        if (!registeredNTDefs.containsKey(nodeTypeName)) {
+            throw new NoSuchNodeTypeException(nodeTypeName.toString());
         }
-        if (builtInNTDefs.contains(name)) {
-            throw new RepositoryException(name.toString()
+        if (builtInNTDefs.contains(nodeTypeName)) {
+            throw new RepositoryException(nodeTypeName.toString()
                     + ": can't unregister built-in node type.");
         }
 
@@ -1225,10 +1162,10 @@
          * check if there are node types that have dependencies on the given
          * node type
          */
-        Set dependentNTs = getDependentNodeTypes(name);
+        Set dependentNTs = getDependentNodeTypes(nodeTypeName);
         if (dependentNTs.size() > 0) {
             StringBuffer msg = new StringBuffer();
-            msg.append(name
+            msg.append(nodeTypeName
                     + " could not be removed because the following node types are referencing
it: ");
             Iterator iterator = dependentNTs.iterator();
             while (iterator.hasNext()) {
@@ -1238,33 +1175,17 @@
             throw new RepositoryException(msg.toString());
         }
 
-        /**
-         * todo
-         * 1. apply deep locks on root nodes in every workspace or alternatively
-         *    put repository in 'single-user' mode
-         * 2. check if the given node type is currently referenced by nodes
-         *    in the repository.
-         * 3. remove the node type if it is not currently referenced, otherwise
-         *    throw exception
-         *
-         * the above checks are absolutely necessary in order to guarantee
-         * integrity of repository content.
-         *
-         * throw exception while this is not implemented properly yet
-         */
-        boolean isReferenced = true;
-        if (isReferenced) {
-            throw new RepositoryException("not yet implemented");
-        }
+        // make sure node type is not currently in use
+        checkForReferencesInContent(nodeTypeName);
 
-        internalUnregister(name);
+        internalUnregister(nodeTypeName);
 
         // persist removal of node type definition
-        customNTDefs.remove(name);
-        persistCustomNTDefs();
+        customNTDefs.remove(nodeTypeName);
+        persistCustomNodeTypeDefs(customNTDefs);
 
         // notify listeners
-        notifyUnregistered(name);
+        notifyUnregistered(nodeTypeName);
     }
 
     /**
@@ -1317,41 +1238,16 @@
             // add new node type definition to store
             customNTDefs.add(ntd);
             // persist node type definitions
-            persistCustomNTDefs();
+            persistCustomNodeTypeDefs(customNTDefs);
 
             // notify listeners
             notifyReRegistered(name);
             return entNew;
         }
 
-        /**
-         * collect names of node types that have dependencies on the given
-         * node type
-         */
-        Set dependentNTs = getDependentNodeTypes(name);
-
-        /**
-         * non-trivial change of node type definition
-         * todo
-         * 1. apply deep locks on root nodes in every workspace or alternatively
-         *    put repository in 'exclusive' or 'single-user' mode
-         * 2. check if the given node type (or any node type that has
-         *    dependencies on this node type) is currently referenced by nodes
-         *    in the repository.
-         * 3. check if applying changes to affected nodes would violate
-         *    existing node type constraints
-         * 4. apply and persist changes to affected nodes (e.g. update
-         *    definition id's, etc.)
-         *
-         * the above checks/actions are absolutely necessary in order to
-         * guarantee integrity of repository content.
-         *
-         * throw exception while this is not implemented properly yet
-         */
-        boolean conflictingContent = true;
-        if (conflictingContent) {
-            throw new RepositoryException("not yet implemented");
-        }
+        // make sure existing content would not conflict
+        // with new node type definition
+        checkForConflictingContent(ntd);
 
         // unregister old node type definition
         internalUnregister(name);
@@ -1361,7 +1257,7 @@
         // persist modified node type definitions
         customNTDefs.remove(name);
         customNTDefs.add(ntd);
-        persistCustomNTDefs();
+        persistCustomNodeTypeDefs(customNTDefs);
 
         // notify listeners
         notifyReRegistered(name);
@@ -1408,7 +1304,7 @@
             throw new NoSuchNodeTypeException(nodeTypeName.toString());
         }
         NodeTypeDef def = (NodeTypeDef) registeredNTDefs.get(nodeTypeName);
-        // return clone to make sure nobody messes around with the 'real' definition
+        // return clone to make sure nobody messes around with the 'live' definition
         try {
             return (NodeTypeDef) def.clone();
         } catch (CloneNotSupportedException e) {
@@ -1426,6 +1322,15 @@
         return registeredNTDefs.containsKey(nodeTypeName);
     }
 
+
+    /**
+     * @param nodeTypeName
+     * @return
+     */
+    public synchronized boolean isBuiltIn(QName nodeTypeName) {
+        return builtInNTDefs.contains(nodeTypeName);
+    }
+
     /**
      * @param id
      * @return
@@ -1530,6 +1435,208 @@
         ps.println();
 
         entCache.dump(ps);
+    }
+
+    //---------------------------------------------------------< overridables >
+    /**
+     * Loads the built-in node type definitions into the given <code>store</code>.
+     * <p/>
+     * This method may be overridden by extensions of this class; It must
+     * only be called once and only from within the constructor though.
+     *
+     * @param store The {@link NodeTypeDefStore} into which the node type
+     *              definitions are loaded.
+     * @throws RepositoryException If an error occurrs while loading the
+     *                             built-in node type definitions.
+     */
+    protected void loadBuiltInNodeTypeDefs(NodeTypeDefStore store)
+            throws RepositoryException {
+        InputStream in = null;
+        try {
+            in = getClass().getClassLoader().getResourceAsStream(BUILTIN_NODETYPES_RESOURCE_PATH);
+            store.load(in);
+        } catch (IOException ioe) {
+            String error =
+                    "internal error: failed to read built-in node type definitions stored
in "
+                    + BUILTIN_NODETYPES_RESOURCE_PATH;
+            log.debug(error);
+            throw new RepositoryException(error, ioe);
+        } catch (InvalidNodeTypeDefException intde) {
+            String error =
+                    "internal error: invalid built-in node type definition stored in "
+                    + BUILTIN_NODETYPES_RESOURCE_PATH;
+            log.debug(error);
+            throw new RepositoryException(error, intde);
+        } finally {
+            if (in != null) {
+                try {
+                    in.close();
+                } catch (IOException ioe) {
+                    // ignore
+                }
+            }
+        }
+    }
+
+    /**
+     * Loads the custom node type definitions into the given <code>store</code>.
+     * <p/>
+     * This method may be overridden by extensions of this class; It must
+     * only be called once and only from within the constructor though.
+     *
+     * @param store The {@link NodeTypeDefStore} into which the node type
+     *              definitions are loaded.
+     * @throws RepositoryException If an error occurrs while loading the
+     *                             custom node type definitions.
+     */
+    protected void loadCustomNodeTypeDefs(NodeTypeDefStore store)
+            throws RepositoryException {
+
+        InputStream in = null;
+        try {
+            if (customNodeTypesResource.exists()) {
+                in = customNodeTypesResource.getInputStream();
+            }
+        } catch (FileSystemException fse) {
+            String error =
+                    "internal error: failed to access custom node type definitions stored
in "
+                    + customNodeTypesResource.getPath();
+            log.debug(error);
+            throw new RepositoryException(error, fse);
+        }
+
+        if (in == null) {
+            log.info("no custom node type definitions found");
+        } else {
+            try {
+                store.load(in);
+            } catch (IOException ioe) {
+                String error =
+                        "internal error: failed to read custom node type definitions stored
in "
+                        + customNodeTypesResource.getPath();
+                log.debug(error);
+                throw new RepositoryException(error, ioe);
+            } catch (InvalidNodeTypeDefException intde) {
+                String error =
+                        "internal error: invalid custom node type definition stored in "
+                        + customNodeTypesResource.getPath();
+                log.debug(error);
+                throw new RepositoryException(error, intde);
+            } finally {
+                try {
+                    in.close();
+                } catch (IOException ioe) {
+                    // ignore
+                }
+            }
+        }
+    }
+
+    /**
+     * Persists the custom node type definitions contained in the given
+     * <code>store</code>.
+     *
+     * @param store The {@link NodeTypeDefStore} containing the definitons to
+     *              be persisted.
+     * @throws RepositoryException If an error occurrs while persisting the
+     *                             custom node type definitions.
+     */
+    protected void persistCustomNodeTypeDefs(NodeTypeDefStore store)
+            throws RepositoryException {
+        OutputStream out = null;
+        try {
+            out = customNodeTypesResource.getOutputStream();
+            customNTDefs.store(out, nsReg);
+        } catch (IOException ioe) {
+            String error =
+                    "internal error: failed to persist custom node type definitions to "
+                    + customNodeTypesResource.getPath();
+            log.debug(error);
+            throw new RepositoryException(error, ioe);
+        } catch (FileSystemException fse) {
+            String error =
+                    "internal error: failed to persist custom node type definitions to "
+                    + customNodeTypesResource.getPath();
+            log.debug(error);
+            throw new RepositoryException(error, fse);
+        } finally {
+            if (out != null) {
+                try {
+                    out.close();
+                } catch (IOException ioe) {
+                    // ignore
+                }
+            }
+        }
+    }
+
+    /**
+     * Checks whether there is existing content that would conflict with the
+     * given node type definition.
+     * <p/>
+     * This method is not implemented yet and always throws a
+     * <code>RepositoryException</code>.
+     * <p/>
+     * TODO
+     * <ol>
+     * <li>apply deep locks on root nodes in every workspace or alternatively
+     * put repository in 'exclusive' or 'single-user' mode
+     * <li>check if the given node type (or any node type that has
+     * dependencies on this node type) is currently referenced by nodes
+     * in the repository.
+     * <li>check if applying the changed definitions to the affected items would
+     * violate existing node type constraints
+     * <li>apply and persist changes to affected nodes (e.g. update
+     * definition id's, etc.)
+     * </ul>
+     * <p/>
+     * the above checks/actions are absolutely necessary in order to
+     * guarantee integrity of repository content.
+     *
+     * @param ntd The node type definition replacing the former node type
+     *            definition of the same name.
+     * @throws RepositoryException If there is conflicting content or if the
+     *                             check failed for some other reason.
+     */
+    protected void checkForConflictingContent(NodeTypeDef ntd)
+            throws RepositoryException {
+        /**
+         * collect names of node types that have dependencies on the given
+         * node type
+         */
+        //Set dependentNTs = getDependentNodeTypes(ntd.getName());
+
+        throw new RepositoryException("not yet implemented");
+    }
+
+    /**
+     * Checks whether there is existing content that directly or indirectly
+     * refers to the specified node type.
+     * <p/>
+     * This method is not implemented yet and always throws a
+     * <code>RepositoryException</code>.
+     * <p/>
+     * TODO:
+     * <ol>
+     * <li>apply deep locks on root nodes in every workspace or alternatively
+     * put repository in 'single-user' mode
+     * <li>check if the given node type is currently referenced by nodes
+     * in the repository.
+     * <li>remove the node type if it is not currently referenced, otherwise
+     * throw exception
+     * </ul>
+     * <p/>
+     * the above checks are absolutely necessary in order to guarantee
+     * integrity of repository content.
+     *
+     * @param nodeTypeName The name of the node type to be checked.
+     * @throws RepositoryException If the specified node type is currently
+     *                             being referenced or if the check failed for
+     *                             some other reason.
+     */
+    protected void checkForReferencesInContent(QName nodeTypeName)
+            throws RepositoryException {
+        throw new RepositoryException("not yet implemented");
     }
 
     //--------------------------------------------------------< inner classes >



Mime
View raw message