KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > commands > MgnlRepositoryCatalog


1 package info.magnolia.commands;
2
3 import info.magnolia.cms.core.Content;
4 import info.magnolia.cms.core.ItemType;
5 import info.magnolia.cms.core.NodeData;
6 import info.magnolia.cms.util.ClassUtil;
7 import info.magnolia.cms.util.ContentUtil;
8
9 import java.util.Collection JavaDoc;
10 import java.util.Iterator JavaDoc;
11
12 import org.apache.commons.chain.Chain;
13 import org.apache.commons.chain.Command;
14 import org.apache.commons.chain.impl.CatalogBase;
15 import org.apache.commons.chain.impl.ChainBase;
16 import org.apache.commons.lang.StringUtils;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20
21 /**
22  * A command catalog using a repository node as a configuration. Chains are supported. Date: Mar 27, 2006 Time: 10:58:22
23  * AM
24  * @author <a HREF="mailto:niko@macnica.com">Nicolas Modrzyk</a>
25  */

26 public class MgnlRepositoryCatalog extends CatalogBase {
27
28     private static Logger log = LoggerFactory.getLogger(MgnlRepositoryCatalog.class);
29
30     static final String JavaDoc CLASS_NODE_DATA = "impl";
31
32     /**
33      * Initialize this catalog based on the configuration found in the repository
34      */

35     public MgnlRepositoryCatalog(Content content) {
36         Iterator JavaDoc iter = content.getChildren(ItemType.CONTENTNODE).iterator();
37         // loop over the command names one by one
38
while (iter.hasNext()) {
39             // get the action node and name
40
Content actionNode = (Content) iter.next();
41             String JavaDoc actionName = actionNode.getName();
42
43             NodeData impl = actionNode.getNodeData(CLASS_NODE_DATA);
44             if (impl != null && impl.getString() != null && !(impl.getString().equals(StringUtils.EMPTY))) {
45
46                 Command command;
47                 try {
48                     command = createCommand(actionNode);
49                     this.addCommand(actionName, command);
50                 }
51                 catch (Exception JavaDoc e) {
52                     log.error("can't initialize command [" + actionName + "]", e);
53                 }
54                 // continue with next action
55
}
56             else {
57                 log.debug("This is a chain");
58
59                 Chain chain = new ChainBase();
60
61                 // consider any command as a chain, makes things easier
62
// we iterate through each subnode and consider this as a command in the chain
63
Collection JavaDoc childrens = actionNode.getChildren(ItemType.CONTENTNODE);
64
65                 log.debug("Found {} children for action {}", Integer.toString(childrens.size()), actionName);
66                 Iterator JavaDoc iterNode = childrens.iterator();
67
68                 Exception JavaDoc e = null;
69                 while (iterNode.hasNext()) {
70                     try {
71                         Content commandNode = (Content) iterNode.next();
72                         Command command = createCommand(commandNode);
73                         chain.addCommand(command);
74                     }
75                     catch (Exception JavaDoc te) {
76                         e = te;
77                         break;
78                     }
79                 }
80
81                 // add the command only if no error was reported
82
if (e != null) {
83                     log.error("Could not load commands for action:" + actionName, e);
84                 }
85                 else {
86                     // add the chain to the catalog linked with the actionName
87
this.addCommand(actionName, chain);
88                 }
89             }
90         }
91    }
92
93     /**
94      * Create a command object based on a node
95      * @param commandNode
96      * @return
97      * @throws ClassNotFoundException
98      * @throws InstantiationException
99      * @throws IllegalAccessException
100      */

101     private Command createCommand(Content commandNode) throws InstantiationException JavaDoc,
102         IllegalAccessException JavaDoc {
103         Command command = null;
104         String JavaDoc className;
105         className = commandNode.getNodeData(CLASS_NODE_DATA).getString();
106
107         log.debug("Found class {} for action {}", className, commandNode.getName());
108         try{
109             Class JavaDoc klass = ClassUtil.classForName(className);
110             command = (Command) klass.newInstance();
111             ContentUtil.setProperties(command, commandNode);
112             return command;
113         }
114         // asume it is a qualified command name
115
catch(ClassNotFoundException JavaDoc e){
116             // this will get the command at runtime from an other catalogue
117
return new DelegateCommand(className);
118         }
119     }
120 }
Popular Tags