Index: src/main/java/info/magnolia/cms/beans/config/VirtualURIManager.java =================================================================== --- src/main/java/info/magnolia/cms/beans/config/VirtualURIManager.java (revision 38534) +++ src/main/java/info/magnolia/cms/beans/config/VirtualURIManager.java (working copy) @@ -34,10 +34,9 @@ package info.magnolia.cms.beans.config; import info.magnolia.cms.core.Content; +import info.magnolia.cms.core.ItemType; +import info.magnolia.content2bean.Content2BeanException; import info.magnolia.content2bean.Content2BeanUtil; -import info.magnolia.content2bean.TransformationState; -import info.magnolia.content2bean.TypeDescriptor; -import info.magnolia.content2bean.impl.Content2BeanTransformerImpl; import info.magnolia.objectfactory.Components; import java.util.ArrayList; @@ -45,11 +44,13 @@ import java.util.Iterator; import java.util.List; +import javax.jcr.RepositoryException; + import org.apache.commons.lang.StringUtils; /** * Store for all virtual URI to template/page mapping. - * + * * @author Sameer Charles * @version 2.0 */ @@ -68,7 +69,7 @@ /** * all cached data. */ - private final List cachedURImapping = new ArrayList(); + private final List cachedURImapping = new ArrayList(); /** * checks for the requested URI mapping in Server config : Servlet Specification 2.3 Section 10 "Mapping Requests to @@ -76,11 +77,11 @@ * @return URI string mapping */ public String getURIMapping(String uri) { - Iterator e = cachedURImapping.iterator(); + Iterator e = cachedURImapping.iterator(); String mappedURI = StringUtils.EMPTY; int lastMatchedLevel = 0; while (e.hasNext()) { - VirtualURIMapping vm = (VirtualURIMapping) e.next(); + VirtualURIMapping vm = e.next(); VirtualURIMapping.MappingResult result = vm.mapURI(uri); if (result != null && lastMatchedLevel < result.getLevel()) { lastMatchedLevel = result.getLevel(); @@ -91,29 +92,58 @@ } protected void onRegister(Content node) { + Collection virtualURIMappingNodes = node.getChildren(ItemType.CONTENTNODE); + for (Content virtualURIMappingNode : virtualURIMappingNodes) { + try { + addVirtualURIMappingToCache(virtualURIMappingNode); + } catch (Exception e) { + log.error("Can't reload the node " + virtualURIMappingNode.getUUID() + " on location: " + virtualURIMappingNode.getHandle()); + } + } + + Collection subFolders = node.getChildren(ItemType.CONTENT); + for (Content subFolder : subFolders) { + // do not register other observations + onRegister(subFolder); + } + } + + protected void addVirtualURIMappingToCache(Content c) { + try { - log.info("Loading VirtualURIMapping from {}", node.getHandle()); //$NON-NLS-1$ - Content2BeanUtil.setProperties(this.cachedURImapping, node, true, new Content2BeanTransformerImpl(){ - protected TypeDescriptor onResolveType(TransformationState state, TypeDescriptor resolvedType) { - if(state.getLevel()==2 && resolvedType == null){ - return this.getTypeMapping().getTypeDescriptor(DefaultVirtualURIMapping.class); + //Checking if all data is there needed for a virtual URI mapping. + if(c.hasNodeData("fromURI") && c.hasNodeData("toURI") && c.hasNodeData("class")){ + if(!c.getNodeData("fromURI").getString().isEmpty() && + !c.getNodeData("toURI").getString().isEmpty() && + !c.getNodeData("class").getString().isEmpty()){ + + //Checking if defined class implemented VirtualURIMapping interface + String classProp = c.getNodeData("class").getString(); + if(VirtualURIMapping.class.isAssignableFrom(Class.forName(classProp))){ + final VirtualURIMapping p = (VirtualURIMapping) Content2BeanUtil.toBean(c, true, VirtualURIMapping.class); + log.debug("Registering VirtualURIMapping [{}] of located in [{}]", c.getName(), c.getHandle()); //$NON-NLS-1$ + cachedURImapping.add(p); + } else { + log.warn("Registering VirtualURIMapping [{}] of located in [{}] was not possible, because property 'class' has invalid value: ."+classProp, c.getName(), c.getHandle()); //$NON-NLS-1$ } - return resolvedType; } - }); - log.debug("VirtualURIMapping loaded from {}", node.getHandle()); //$NON-NLS-1$ - } - catch (Exception e) { - log.error("Failed to load VirtualURIMapping from " + node.getHandle() + " - " + e.getMessage(), e); //$NON-NLS-1$ //$NON-NLS-2$ + } + } catch (Content2BeanException e) { + throw new RuntimeException(e); // TODO + } catch (RepositoryException e) { + throw new RuntimeException(e); // TODO + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); // TODO } } + protected void onClear() { this.cachedURImapping.clear(); } // TODO : should this really be public ? - public Collection getURIMappings() { + public Collection getURIMappings() { return cachedURImapping; }