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 ; 10 import java.util.Iterator ; 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 26 public class MgnlRepositoryCatalog extends CatalogBase { 27 28 private static Logger log = LoggerFactory.getLogger(MgnlRepositoryCatalog.class); 29 30 static final String CLASS_NODE_DATA = "impl"; 31 32 35 public MgnlRepositoryCatalog(Content content) { 36 Iterator iter = content.getChildren(ItemType.CONTENTNODE).iterator(); 37 while (iter.hasNext()) { 39 Content actionNode = (Content) iter.next(); 41 String 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 e) { 52 log.error("can't initialize command [" + actionName + "]", e); 53 } 54 } 56 else { 57 log.debug("This is a chain"); 58 59 Chain chain = new ChainBase(); 60 61 Collection childrens = actionNode.getChildren(ItemType.CONTENTNODE); 64 65 log.debug("Found {} children for action {}", Integer.toString(childrens.size()), actionName); 66 Iterator iterNode = childrens.iterator(); 67 68 Exception 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 te) { 76 e = te; 77 break; 78 } 79 } 80 81 if (e != null) { 83 log.error("Could not load commands for action:" + actionName, e); 84 } 85 else { 86 this.addCommand(actionName, chain); 88 } 89 } 90 } 91 } 92 93 101 private Command createCommand(Content commandNode) throws InstantiationException , 102 IllegalAccessException { 103 Command command = null; 104 String className; 105 className = commandNode.getNodeData(CLASS_NODE_DATA).getString(); 106 107 log.debug("Found class {} for action {}", className, commandNode.getName()); 108 try{ 109 Class klass = ClassUtil.classForName(className); 110 command = (Command) klass.newInstance(); 111 ContentUtil.setProperties(command, commandNode); 112 return command; 113 } 114 catch(ClassNotFoundException e){ 116 return new DelegateCommand(className); 118 } 119 } 120 } | Popular Tags |