KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > factories > NodeFactory


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.factories;
8
9 import org.jboss.cache.CacheSPI;
10 import org.jboss.cache.Fqn;
11 import org.jboss.cache.Node;
12 import org.jboss.cache.NodeSPI;
13 import org.jboss.cache.UnversionedNode;
14 import org.jboss.cache.VersionedNode;
15 import org.jboss.cache.optimistic.TransactionWorkspace;
16 import org.jboss.cache.optimistic.WorkspaceNode;
17 import org.jboss.cache.optimistic.WorkspaceNodeImpl;
18
19 import java.util.Map JavaDoc;
20
21 /**
22  * A factory that generates nodes.
23  *
24  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
25  */

26 public class NodeFactory
27 {
28    public enum NodeType
29    {
30       UNVERSIONED_NODE, VERSIONED_NODE, WORKSPACE_NODE
31    }
32
33    private CacheSPI cache;
34    private boolean optimistic;
35
36    /**
37     * Constructs an instance of the factory
38     */

39    public NodeFactory(CacheSPI cache)
40    {
41       this.cache = cache;
42       init();
43    }
44
45    /**
46     * Initialises the node factory with the configuration from the cache.
47     */

48    public void init()
49    {
50       optimistic = cache.getConfiguration().isNodeLockingOptimistic();
51    }
52
53
54    /**
55     * Creates a new {@link Node} instance.
56     *
57     * @param childName the new node's name
58     * @param fqn the new node's Fqn
59     * @param parent the new node's parent
60     * @param data the new node's attribute map
61     * @param mapSafe <code>true</code> if param <code>data</code> can safely
62     * be directly assigned to the new node's data field;
63     * <code>false</code> if param <code>data</code>'s contents
64     * should be copied into the new node's data field.
65     * @return the new node
66     */

67    public NodeSPI createDataNode(Object JavaDoc childName, Fqn fqn, NodeSPI parent, Map JavaDoc data, boolean mapSafe)
68    {
69       return optimistic ? new VersionedNode(childName, fqn, parent, data, cache) : new UnversionedNode(childName, fqn, data, mapSafe, cache);
70    }
71
72    public Node createNode(Object JavaDoc childName, Node parent, Map JavaDoc data)
73    {
74       return createNodeOfType(parent, childName, parent, data);
75    }
76
77    public Node createNodeOfType(Node template, Object JavaDoc childName, Node parent, Map JavaDoc data)
78    {
79       if (template instanceof WorkspaceNode)
80       {
81          NodeSPI dataNodeParent = ((WorkspaceNode) parent).getNode();
82          TransactionWorkspace workspace = ((WorkspaceNode) template).getTransactionWorkspace();
83          return createWorkspaceNode(dataNodeParent, workspace);
84       }
85
86       // not a workspace node.
87
return createDataNode(childName, new Fqn(parent.getFqn(), childName), (NodeSPI) parent, data, false);
88    }
89
90    public WorkspaceNode createWorkspaceNode(NodeSPI dataNode, TransactionWorkspace workspace)
91    {
92       return new WorkspaceNodeImpl(dataNode, workspace);
93    }
94
95    public NodeSPI createRootDataNode()
96    {
97       return createDataNode(null, Fqn.ROOT, null, null, false);
98    }
99
100 }
101
Popular Tags