1 12 package info.magnolia.cms.gui.dialog; 13 14 import info.magnolia.cms.beans.config.ContentRepository; 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.core.ContentHandler; 17 import info.magnolia.cms.core.HierarchyManager; 18 import info.magnolia.cms.core.ItemType; 19 20 import java.util.Iterator ; 21 22 import javax.jcr.RepositoryException; 23 import javax.jcr.ValueFactory; 24 import javax.jcr.observation.Event; 25 import javax.jcr.observation.EventIterator; 26 import javax.jcr.observation.EventListener; 27 import javax.jcr.observation.ObservationManager; 28 29 import org.apache.commons.lang.StringUtils; 30 import org.apache.log4j.Logger; 31 32 33 37 public final class DialogManager { 38 39 42 protected static Logger log = Logger.getLogger(DialogManager.class); 43 44 47 private static final String DIALOGCONTROLS_CONFIG_NAME = "controls"; 49 52 private static final String ADMIN_CONFIG_NODE_NAME = "/modules/adminInterface/Config"; 54 57 private static final String DATA_CONTROL_CLASS = "class"; 59 62 private static final String DATA_CONTROL_NAME = "name"; 64 67 private DialogManager() { 68 } 70 71 74 public static void init() { 75 load(); 76 registerEventListener(); 77 } 78 79 82 private static void registerEventListener() { 83 84 log.info("Registering event listener for Controls"); 86 try { 87 ObservationManager observationManager = ContentRepository 88 .getHierarchyManager(ContentRepository.CONFIG) 89 .getWorkspace() 90 .getObservationManager(); 91 92 observationManager.addEventListener( 93 new EventListener() { 94 95 public void onEvent(EventIterator iterator) { 96 reload(); 98 } 99 }, 100 Event.NODE_ADDED 101 | Event.NODE_REMOVED 102 | Event.PROPERTY_ADDED 103 | Event.PROPERTY_CHANGED 104 | Event.PROPERTY_REMOVED, 105 ADMIN_CONFIG_NODE_NAME + "/" + DIALOGCONTROLS_CONFIG_NAME, true, null, null, false); } 107 catch (RepositoryException e) { 108 log.error("Unable to add event listeners for Controls", e); } 110 } 111 112 115 public static void reload() { 116 load(); 117 } 118 119 122 public static void load() { 123 log.info("Config : loading dialog controls configuration"); 125 HierarchyManager configHierarchyManager = ContentRepository.getHierarchyManager(ContentRepository.CONFIG); 127 128 try { 129 Content serverNode = configHierarchyManager.getContent(ADMIN_CONFIG_NODE_NAME); 130 131 Content configNode; 132 try { 133 configNode = serverNode.getContent(DIALOGCONTROLS_CONFIG_NAME); 134 } 135 catch (javax.jcr.PathNotFoundException e) { 136 137 log.info("Initialize default configuration for dialog controls"); 139 configNode = serverNode.createContent(DIALOGCONTROLS_CONFIG_NAME, ItemType.CONTENT); 140 141 ValueFactory valueFactory = configHierarchyManager.getWorkspace().getSession().getValueFactory(); 142 143 Content fckedit = configNode.createContent("fckEdit", ItemType.CONTENTNODE); fckedit.createNodeData(DATA_CONTROL_NAME, valueFactory.createValue("fckEdit")); fckedit.createNodeData(DATA_CONTROL_CLASS, valueFactory.createValue(DialogFckEdit.class.getName())); 147 configHierarchyManager.save(); 148 } 149 150 if (configNode == null) { 151 return; 152 } 153 154 Iterator iterator = configNode 155 .getChildren(ItemType.CONTENTNODE, ContentHandler.SORT_BY_SEQUENCE) 156 .iterator(); 157 158 while (iterator.hasNext()) { 159 Content controlNode = (Content) iterator.next(); 160 161 if (log.isDebugEnabled()) { 162 log.debug("Initializing control [" + controlNode + "]"); } 164 165 String classNodeData = controlNode.getNodeData(DATA_CONTROL_CLASS).getString(); 166 String nameNodeData = controlNode.getNodeData(DATA_CONTROL_NAME).getString(); 167 168 if (StringUtils.isEmpty(classNodeData) || StringUtils.isEmpty(nameNodeData)) { 169 log.warn("Config : Can't add custom control with name [" + nameNodeData + "] and class [" + classNodeData + "] specified in node [" + controlNode.getName() + "]"); 174 continue; 175 } 176 Class controlClass = null; 177 178 try { 179 controlClass = Class.forName(classNodeData); 180 } 181 catch (ClassNotFoundException e) { 182 log.error("Config : Failed to load dialog control with class [" + classNodeData, e); continue; 184 } 185 186 if (!DialogInterface.class.isAssignableFrom(controlClass)) { 187 log.error("Config : Invalid class specified for control [" + nameNodeData + "]: does not implement DialogInterface"); continue; 190 } 191 192 DialogFactory.registerDialog(nameNodeData, controlClass); 193 194 } 195 196 } 197 catch (RepositoryException e) { 198 log.error("Config : Failed to load dialog controls configuration - " + ADMIN_CONFIG_NODE_NAME + "/" + DIALOGCONTROLS_CONFIG_NAME, e); 201 } 202 203 } 204 205 } 206 | Popular Tags |