1 17 18 package org.sape.carbon.services.config.jndi; 19 20 import javax.naming.Name ; 21 import javax.naming.NameNotFoundException ; 22 import javax.naming.NamingException ; 23 import javax.naming.directory.Attributes ; 24 import javax.naming.directory.DirContext ; 25 26 import org.sape.carbon.core.config.format.ConfigurationFormatService; 27 import org.sape.carbon.core.config.format.DefaultConfigurationFormatService; 28 import org.sape.carbon.core.config.node.Node; 29 import org.sape.carbon.core.config.node.NodeCreationException; 30 import org.sape.carbon.core.config.node.NodeFactory; 31 import org.sape.carbon.core.exception.InvalidParameterException; 32 33 44 public abstract class AbstractJNDIConfigurationDocumentFactory 45 implements NodeFactory { 46 47 private static final ConfigurationFormatService FORMAT_SERVICE = 48 new DefaultConfigurationFormatService(); 49 50 protected JNDILinkNodeConfiguration config; 51 52 58 public AbstractJNDIConfigurationDocumentFactory( 59 JNDILinkNodeConfiguration config) { 60 61 if (config == null) { 62 throw new InvalidParameterException( 63 this.getClass(), 64 "config cannot be null"); 65 } 66 67 this.config = config; 68 } 69 70 80 public Node getInstance(Node parent, String name) 81 throws NodeCreationException { 82 83 final String NAME = this.config.getNodeNameAttributeName(); 85 final String EQUALS = this.config.getAttributeNameValueSeparator(); 86 final String NODE_TYPE = this.config.getNodeTypeAttributeName(); 87 final String FOLDER = this.config.getFolderNodeTypeAttributeValue(); 88 final String DOCUMENT = this.config.getDocumentNodeTypeAttributeValue(); 89 final String DOC_TYPE = this.config.getDocumentTypeAttributeName(); 90 final String CONTENT = this.config.getDocumentContectAttributeName(); 91 92 if (name == null) { 93 throw new InvalidParameterException( 94 this.getClass(),"name parameter cannot be null"); 95 } 96 97 JNDIFolder parentJNDIFolder; 98 try { 99 parentJNDIFolder = (JNDIFolder) parent; 100 } catch(ClassCastException cce) { 101 throw new InvalidParameterException( 102 this.getClass(), 103 "parent is not assignable from JNDIFolder", cce); 104 } 105 106 try { 107 Name nodeContextName = 109 (Name ) parentJNDIFolder.getNodeContextName().clone(); 110 nodeContextName.add(NAME + EQUALS + name); 111 112 DirContext initialContext = parentJNDIFolder.getInitialContext(); 113 114 try { 115 String [] docAttributesIDs = new String [] { NODE_TYPE, DOC_TYPE }; 117 Attributes docAttributes = initialContext.getAttributes( 118 nodeContextName, 119 docAttributesIDs); 120 121 if (!docAttributes.get(NODE_TYPE).contains(DOCUMENT)) { 122 throw new NodeCreationException( 124 this.getClass(),parent,name, 125 "Context [" 126 + nodeContextName.toString() 127 + "] exists, but is missing required document attribute: [" 128 + NODE_TYPE 129 + EQUALS 130 + DOCUMENT 131 + "]"); 132 } 133 if (!docAttributes.get(DOC_TYPE).contains(getTypeAttributeValue())) { 134 throw new NodeCreationException( 136 this.getClass(),parent,name, 137 "Context [" 138 + nodeContextName.toString() 139 + "] exists, but has incorrect type, expected type: [" 140 + getTypeAttributeValue() 141 + "], actual type: [" 142 + docAttributes.get(DOC_TYPE) 143 + "]"); 144 } 145 146 } catch (NameNotFoundException nnfe) { 147 } 150 151 return new JNDIConfigurationDocument( 153 parent, 154 name, 155 getConfigurationFormatService(), 156 parentJNDIFolder.getInitialContext(), 157 nodeContextName, 158 this.config, 159 getTypeAttributeValue()); 160 161 } catch (NamingException ne) { 162 throw new NodeCreationException( 163 this.getClass(),parent,name, 164 "Problem reading JNDI directory", 165 ne); 166 } 167 } 168 169 177 protected abstract String getTypeAttributeValue(); 178 179 190 protected ConfigurationFormatService getConfigurationFormatService() { 191 return AbstractJNDIConfigurationDocumentFactory.FORMAT_SERVICE; 192 } 193 } | Popular Tags |