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.BasicAttributes ; 25 import javax.naming.directory.DirContext ; 26 27 import org.sape.carbon.core.config.node.Node; 28 import org.sape.carbon.core.config.node.NodeCreationException; 29 import org.sape.carbon.core.config.node.NodeFactory; 30 import org.sape.carbon.core.exception.InvalidParameterException; 31 32 33 41 public class JNDIFolderFactory 42 implements NodeFactory { 43 44 private JNDILinkNodeConfiguration config; 45 46 52 public JNDIFolderFactory(JNDILinkNodeConfiguration config) { 53 if (config == null) { 54 throw new InvalidParameterException( 55 this.getClass(), 56 "config cannot be null"); 57 } 58 59 this.config = config; 60 } 61 62 72 public Node getInstance(Node parent, String name) 73 throws NodeCreationException { 74 75 final String NAME = this.config.getNodeNameAttributeName(); 77 final String EQUALS = this.config.getAttributeNameValueSeparator(); 78 final String NODE_TYPE = this.config.getNodeTypeAttributeName(); 79 final String FOLDER = this.config.getFolderNodeTypeAttributeValue(); 80 81 JNDIFolder parentJNDIFolder; 82 try { 83 parentJNDIFolder = (JNDIFolder) parent; 84 } catch(ClassCastException cce) { 85 throw new InvalidParameterException( 86 this.getClass(), 87 "parent is not assignable from JNDIFolder", cce); 88 } 89 90 if (name == null) { 91 throw new InvalidParameterException( 92 this.getClass(), 93 "name cannot be null"); 94 } 95 96 try { 97 Name nodeContextName = 99 (Name ) parentJNDIFolder.getNodeContextName().clone(); 100 nodeContextName.add(NAME + EQUALS + name); 101 102 DirContext initialContext = parentJNDIFolder.getInitialContext(); 103 104 try { 105 Attributes folderAttributes = initialContext.getAttributes( 107 nodeContextName, 108 new String [] { NODE_TYPE }); 109 110 if (!folderAttributes.get(NODE_TYPE).contains(FOLDER)) { 111 throw new NodeCreationException( 113 this.getClass(), 114 parent, 115 name, 116 "Context [" 117 + nodeContextName.toString() 118 + "] exists, but is missing required folder attribute [" 119 + NODE_TYPE 120 + EQUALS 121 + FOLDER 122 + "]"); 123 } 124 125 } catch (NameNotFoundException nnfe) { 126 Attributes folderAttributes = 128 new BasicAttributes (NODE_TYPE, FOLDER); 129 130 folderAttributes.put(NAME, name); 131 132 initialContext.createSubcontext(nodeContextName, folderAttributes); 133 } 134 135 return new JNDIFolder( 137 parent, 138 name, 139 parentJNDIFolder.getSubFolderFactory(), 140 parentJNDIFolder.getConfigurationDocumentFactory(), 141 parentJNDIFolder.getLinkNodeFactory(), 142 initialContext, 143 nodeContextName, 144 this.config); 145 146 } catch (NamingException ne) { 147 throw new NodeCreationException( 148 this.getClass(), 149 parent, 150 name, 151 "Problem reading JNDI directory", 152 ne); 153 } 154 } 155 } | Popular Tags |