1 17 18 package org.sape.carbon.services.config.jndi; 19 20 import java.util.Hashtable ; 21 22 import javax.naming.Context ; 23 import javax.naming.Name ; 24 import javax.naming.NameParser ; 25 import javax.naming.NamingException ; 26 import javax.naming.directory.Attribute ; 27 import javax.naming.directory.Attributes ; 28 import javax.naming.directory.DirContext ; 29 import javax.naming.directory.InitialDirContext ; 30 31 import org.sape.carbon.core.config.InvalidConfigurationException; 32 import org.sape.carbon.core.config.format.ConfigurationFormatException; 33 import org.sape.carbon.core.config.format.DefaultConfigurationFormatService; 34 import org.sape.carbon.core.config.node.ConfigurationDocument; 35 import org.sape.carbon.core.config.node.Node; 36 import org.sape.carbon.core.config.node.NodeCreationException; 37 import org.sape.carbon.core.config.node.NodeFactory; 38 import org.sape.carbon.core.config.node.NodeIOException; 39 import org.sape.carbon.core.config.node.link.ConfigurationDocumentLinkImpl; 40 import org.sape.carbon.core.config.node.link.FolderLinkImpl; 41 import org.sape.carbon.core.config.node.link.LinkNode; 42 import org.sape.carbon.core.config.node.link.LinkNodeFactory; 43 44 import org.apache.commons.logging.Log; 45 import org.apache.commons.logging.LogFactory; 46 47 48 56 public class JNDILinkNodeFactory implements LinkNodeFactory { 57 58 private Log log = LogFactory.getLog(this.getClass()); 59 60 63 public LinkNode getInstance(Node parent, String name, 64 ConfigurationDocument linkConfigurationDoc) 65 throws NodeCreationException { 66 67 try { 68 JNDILinkNodeConfiguration config = (JNDILinkNodeConfiguration) 69 linkConfigurationDoc.readConfiguration(); 70 71 validateConfig(config); 72 73 DirContext initialContext = (DirContext ) 75 config.getInitialContextFactory().getDirContext(); 76 77 NameParser nameParser = initialContext.getNameParser(""); 79 80 Name initialContextName = 81 nameParser.parse(config.getTargetContextName()); 82 83 Attributes targetAttributes = 85 initialContext.getAttributes( 86 initialContextName, 87 new String [] { config.getNodeTypeAttributeName() }); 88 89 Attribute typeID = 90 targetAttributes.get(config.getNodeTypeAttributeName()); 91 92 if (typeID.contains(config.getFolderNodeTypeAttributeValue())) { 94 return new FolderLinkImpl( 95 linkConfigurationDoc, 96 new JNDIFolder( 97 parent, 98 name, 99 getSubFolderFactory(config), 100 getConfigurationDocumentFactory(config), 101 getLinkNodeFactory(config), 102 initialContext, 103 initialContextName, 104 config)); 105 } 106 107 if (typeID.contains(config.getDocumentNodeTypeAttributeValue())) { 108 return new ConfigurationDocumentLinkImpl( 109 linkConfigurationDoc, 110 new JNDIConfigurationDocument( 111 parent, 112 name, 113 new DefaultConfigurationFormatService(), 114 initialContext, 115 initialContextName, 116 config, 117 config.getLinkDocumentTypeAttributeValue())); 118 } 119 120 throw new NodeCreationException( 121 this.getClass(), 122 parent, 123 name, 124 "InitialContextName [" 125 + config.getTargetContextName() 126 + "] did not refer to a document or folder"); 127 128 } catch (NodeIOException nioe) { 129 throw new NodeCreationException( 130 this.getClass(), 131 parent, 132 name, 133 "Could not read link configuration", 134 nioe); 135 136 } catch (ConfigurationFormatException cfe) { 137 throw new NodeCreationException( 138 this.getClass(), 139 parent, 140 name, 141 "Could not read link configuration", 142 cfe); 143 144 } catch (ClassCastException cce) { 145 throw new NodeCreationException( 146 this.getClass(), 147 parent, 148 name, 149 "Link configuration was not of type " 150 + JNDILinkNodeConfiguration.class.getName(), 151 cce); 152 153 } catch (NamingException ne) { 154 throw new NodeCreationException( 155 this.getClass(), 156 parent, 157 name, 158 "Problem reading JNDI directory", 159 ne); 160 } 161 } 162 163 168 protected NodeFactory getSubFolderFactory(JNDILinkNodeConfiguration config) { 169 return new JNDIFolderFactory(config); 170 } 171 172 178 protected NodeFactory getConfigurationDocumentFactory(JNDILinkNodeConfiguration config) { 179 return new JNDIConfigurationDocumentFactory(config); 180 } 181 182 187 protected NodeFactory getLinkNodeFactory(JNDILinkNodeConfiguration config) { 188 return new JNDILinkNodeConfigurationDocumentFactory(config); 189 } 190 191 195 private void validateConfig(JNDILinkNodeConfiguration config) { 196 if (config.getInitialContextFactory() == null) { 197 throw new InvalidConfigurationException( 198 this.getClass(), 199 config.getConfigurationName(), 200 "InitialContextFactory", 201 "InitialContextFactory must be specified"); 202 } 203 204 if (config.getTargetContextName() == null || 205 config.getTargetContextName().equals("")) { 206 throw new InvalidConfigurationException( 207 this.getClass(), 208 config.getConfigurationName(), 209 "InitialContextName", 210 "InitialContextName must be specified"); 211 } 212 } 213 214 } | Popular Tags |