KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > corebuilders > OAlias


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.module.corebuilders;
11
12 import java.util.*;
13 import org.mmbase.cache.Cache;
14 import org.mmbase.module.core.*;
15 import org.mmbase.storage.search.*;
16 import org.mmbase.storage.search.implementation.*;
17
18
19 import org.mmbase.util.logging.Logger;
20 import org.mmbase.util.logging.Logging;
21
22 /**
23  * The OAlias builder is an optional corebuilder used to associate aliases with nodes.
24  * Each OAlias object contains a name field (the alias), and a destination field (the number of
25  * the object referenced).
26  * This builder is not used directly. If you add aliases, use {@link MMObjectBuilder#createAlias} instead
27  * of the builder's insert method.
28  * MMBase will run without this builder, but most applications use aliases.
29  *
30  * @author Rico Jansen
31  * @author Michiel Meeuwissen
32  * @version $Id: OAlias.java,v 1.20 2006/03/24 13:36:59 johannes Exp $
33  */

34
35 public class OAlias extends MMObjectBuilder {
36
37     private static final Logger log = Logging.getLoggerInstance(OAlias.class);
38
39     // alias -> node-number (Integer)
40
private Cache numberCache = new Cache(128) {
41         public String JavaDoc getName() { return "AliasCache"; }
42         public String JavaDoc getDescription() { return "Cache for node aliases"; }
43         };
44
45     private static final Integer JavaDoc NOT_FOUND = new Integer JavaDoc(-1);
46
47     public OAlias() {
48         numberCache.putCache();
49     }
50
51     public boolean init() {
52         boolean res = super.init();
53         if (res) checkAddTmpField("_destination");
54         return res;
55     }
56
57     /**
58      * Obtain the number of a node through its alias
59      * @param name the alias of the desired node
60      * @return the number of the node, or -1 if the alias does not exist
61      * @see #getAliasedNode
62      */

63     public int getNumber(String JavaDoc name) {
64         if (log.isDebugEnabled()) {
65             log.debug("Finding oalias node '" + name + "'");
66         }
67
68         Integer JavaDoc nodeNumber = (Integer JavaDoc) numberCache.get(name);
69         if (nodeNumber == null) {
70             try {
71                 NodeSearchQuery query = new NodeSearchQuery(this);
72                 BasicFieldValueConstraint constraint = new BasicFieldValueConstraint(query.getField(getField("name")), name);
73                 query.setConstraint(constraint);
74                 Iterator i = getNodes(query).iterator();
75                 if (i.hasNext()) {
76                     MMObjectNode node = (MMObjectNode) i.next();
77                     int rtn = node.getIntValue("destination");
78                     numberCache.put(name, new Integer JavaDoc(rtn));
79                     return rtn;
80                 } else {
81                     numberCache.put(name, NOT_FOUND);
82                     return -1;
83                 }
84             } catch (SearchQueryException sqe) {
85                 log.error(sqe.toString());
86                 return -1;
87             }
88         } else {
89             return nodeNumber.intValue();
90         }
91     }
92
93     /**
94      * Obtain the alias of a node. If a node has more aliases, it returns only one.
95      * Which one is not specified.
96      * @param number the number of the node
97      * @return the alias of the node, or null if it does not exist
98      * @see #getNumber
99      * @todo No caching here?
100      */

101     public String JavaDoc getAlias(int number) {
102         NodeSearchQuery query = new NodeSearchQuery(this);
103         BasicFieldValueConstraint constraint = new BasicFieldValueConstraint(query.getField(getField("destination")), new Integer JavaDoc(number));
104         query.setConstraint(constraint);
105         try {
106             Iterator i = getNodes(query).iterator();
107             if (i.hasNext()) {
108                 MMObjectNode node = (MMObjectNode)i.next();
109                 return node.getStringValue("name");
110             } else {
111                 return null;
112             }
113         } catch (SearchQueryException sqe) {
114             log.error(sqe.toString());
115             return null;
116
117         }
118     }
119
120     /**
121      * Obtain a node from the cloud through its alias
122      * @param alias the alias of the desired node
123      * @return the node, or null if the alias does not exist
124      * @throws RuntimeException if the alias exists but the node itself doesn't (this indicates
125      * an inconsistency in the database)
126      * @see #getNumber
127      */

128     public MMObjectNode getAliasedNode(String JavaDoc alias) {
129         MMObjectNode node = null;
130         int nr = getNumber(alias);
131         if (nr > 0) {
132             try {
133                 node = getNode(nr);
134             } catch (RuntimeException JavaDoc e) {
135                 log.error("Alias '" + alias + "' points to non-existing node with number " + nr);
136                 throw e;
137             }
138         }
139         return node;
140     }
141
142     /**
143      * Creates an alias for the node with the given number, and updates the alias cache.
144      *
145      * @since MMBase-1.7
146      */

147     public void createAlias(String JavaDoc alias, int number) {
148         createAlias(alias, number, "system");
149     }
150
151     /**
152      * Creates an alias for the node with the given number, and updates the alias cache.
153      *
154      * @since MMBase-1.8
155      */

156     public void createAlias(String JavaDoc alias, int number, String JavaDoc owner) {
157         MMObjectNode node = getNewNode(owner);
158         node.setValue("name", alias);
159         node.setValue("destination", number);
160         node.insert(owner);
161         numberCache.remove(alias);
162     }
163
164     /**
165      * Remove a node from the cloud and update the cache
166      * @param node The node to remove.
167      */

168     public void removeNode(MMObjectNode node) {
169         String JavaDoc name = node.getStringValue("name");
170         super.removeNode(node);
171         numberCache.remove(name);
172     }
173
174     /**
175      * {@inheritDoc}
176      * If a node is changed or newly created, this adds the new or updated alias to the
177      * cache.
178      * @since MMBase-1.7.1
179      */

180     public boolean nodeRemoteChanged(String JavaDoc machine, String JavaDoc number, String JavaDoc builder, String JavaDoc ctype) {
181         if (builder.equals(getTableName())) {
182             if (ctype.equals("c") || ctype.equals("n")) {
183                 // should remove aliasses referencing this number from numberCache here
184
MMObjectNode node = getNode(number);
185                 numberCache.put(node.getStringValue("name"), node.getIntegerValue("destination"));
186             } else if (ctype.equals("d")) {
187                 Integer JavaDoc n = new Integer JavaDoc(number);
188                 Iterator i = numberCache.entrySet().iterator();
189                 while (i.hasNext()) {
190                     Map.Entry entry = (Map.Entry) i.next();
191                     Object JavaDoc value = entry.getValue();
192                     if (n.equals(value)) {
193                         i.remove();
194                     }
195                 }
196             }
197        }
198        return super.nodeRemoteChanged(machine, number, builder, ctype);
199     }
200
201 }
202
Popular Tags