001 /*
002 * JBoss DNA (http://www.jboss.org/dna)
003 * See the COPYRIGHT.txt file distributed with this work for information
004 * regarding copyright ownership. Some portions may be licensed
005 * to Red Hat, Inc. under one or more contributor license agreements.
006 * See the AUTHORS.txt file in the distribution for a full listing of
007 * individual contributors.
008 *
009 * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010 * is licensed to you under the terms of the GNU Lesser General Public License as
011 * published by the Free Software Foundation; either version 2.1 of
012 * the License, or (at your option) any later version.
013 *
014 * JBoss DNA is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017 * Lesser General Public License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this software; if not, write to the Free
021 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023 */
024 package org.jboss.dna.repository.util;
025
026 import java.util.Collections;
027 import java.util.HashSet;
028 import java.util.Set;
029 import javax.jcr.RepositoryException;
030 import javax.jcr.Session;
031 import org.jboss.dna.common.util.CheckArg;
032 import org.jboss.dna.graph.property.NamespaceException;
033 import org.jboss.dna.graph.property.NamespaceRegistry;
034 import org.jboss.dna.graph.property.basic.BasicNamespace;
035
036 /**
037 * @author Randall Hauch
038 */
039 public class JcrNamespaceRegistry implements NamespaceRegistry {
040
041 private final String repositoryWorkspaceName;
042 private final SessionFactory sessionFactory;
043
044 public JcrNamespaceRegistry( SessionFactory sessionFactory,
045 String repositoryWorkspaceName ) {
046 CheckArg.isNotNull(sessionFactory, "sessionFactory");
047 CheckArg.isNotNull(repositoryWorkspaceName, "repositoryWorkspaceName");
048 this.repositoryWorkspaceName = repositoryWorkspaceName;
049 this.sessionFactory = sessionFactory;
050 }
051
052 /**
053 * {@inheritDoc}
054 */
055 public String getDefaultNamespaceUri() {
056 Session session = null;
057 try {
058 session = this.sessionFactory.createSession(this.repositoryWorkspaceName);
059 return session.getNamespaceURI("");
060 } catch (RepositoryException e) {
061 throw new NamespaceException(e);
062 } finally {
063 if (session != null) {
064 session.logout();
065 }
066 }
067 }
068
069 /**
070 * {@inheritDoc}
071 */
072 public String getNamespaceForPrefix( String prefix ) {
073 Session session = null;
074 try {
075 session = this.sessionFactory.createSession(this.repositoryWorkspaceName);
076 return session.getNamespaceURI(prefix);
077 } catch (RepositoryException e) {
078 throw new NamespaceException(e);
079 } finally {
080 if (session != null) {
081 session.logout();
082 }
083 }
084 }
085
086 /**
087 * {@inheritDoc}
088 */
089 public String getPrefixForNamespaceUri( String namespaceUri,
090 boolean generateIfMissing ) {
091 Session session = null;
092 try {
093 session = this.sessionFactory.createSession(this.repositoryWorkspaceName);
094 return session.getNamespacePrefix(namespaceUri);
095 } catch (RepositoryException e) {
096 throw new NamespaceException(e);
097 } finally {
098 if (session != null) {
099 session.logout();
100 }
101 }
102 }
103
104 /**
105 * {@inheritDoc}
106 */
107 public boolean isRegisteredNamespaceUri( String namespaceUri ) {
108 Session session = null;
109 try {
110 session = this.sessionFactory.createSession(this.repositoryWorkspaceName);
111 session.getNamespacePrefix(namespaceUri);
112 return true;
113 } catch (javax.jcr.NamespaceException e) {
114 return false;
115 } catch (RepositoryException e) {
116 throw new NamespaceException(e);
117 } finally {
118 if (session != null) {
119 session.logout();
120 }
121 }
122 }
123
124 /**
125 * {@inheritDoc}
126 */
127 public String register( String prefix,
128 String namespaceUri ) {
129 String previousNamespaceUriForPrefix = null;
130 Session session = null;
131 try {
132 session = this.sessionFactory.createSession(this.repositoryWorkspaceName);
133 previousNamespaceUriForPrefix = session.getNamespacePrefix(namespaceUri);
134 javax.jcr.NamespaceRegistry registry = session.getWorkspace().getNamespaceRegistry();
135 registry.registerNamespace(prefix, namespaceUri);
136 } catch (RepositoryException e) {
137 throw new NamespaceException(e);
138 } finally {
139 if (session != null) {
140 session.logout();
141 }
142 }
143 return previousNamespaceUriForPrefix;
144 }
145
146 /**
147 * {@inheritDoc}
148 *
149 * @see org.jboss.dna.graph.property.NamespaceRegistry#unregister(java.lang.String)
150 */
151 public boolean unregister( String namespaceUri ) {
152 Session session = null;
153 try {
154 session = this.sessionFactory.createSession(this.repositoryWorkspaceName);
155 String prefix = session.getNamespacePrefix(namespaceUri);
156 javax.jcr.NamespaceRegistry registry = session.getWorkspace().getNamespaceRegistry();
157 registry.unregisterNamespace(prefix);
158 } catch (javax.jcr.NamespaceException e) {
159 return false;
160 } catch (RepositoryException e) {
161 throw new NamespaceException(e);
162 } finally {
163 if (session != null) {
164 session.logout();
165 }
166 }
167 return true;
168 }
169
170 /**
171 * {@inheritDoc}
172 */
173 public Set<String> getRegisteredNamespaceUris() {
174 Session session = null;
175 try {
176 session = this.sessionFactory.createSession(this.repositoryWorkspaceName);
177 javax.jcr.NamespaceRegistry registry = session.getWorkspace().getNamespaceRegistry();
178 Set<String> result = new HashSet<String>();
179 for (String uri : registry.getURIs()) {
180 result.add(uri);
181 }
182 return Collections.unmodifiableSet(result);
183 } catch (RepositoryException e) {
184 throw new NamespaceException(e);
185 } finally {
186 if (session != null) {
187 session.logout();
188 }
189 }
190 }
191
192 /**
193 * {@inheritDoc}
194 *
195 * @see org.jboss.dna.graph.property.NamespaceRegistry#getNamespaces()
196 */
197 public Set<Namespace> getNamespaces() {
198 Session session = null;
199 try {
200 session = this.sessionFactory.createSession(this.repositoryWorkspaceName);
201 javax.jcr.NamespaceRegistry registry = session.getWorkspace().getNamespaceRegistry();
202 Set<Namespace> result = new HashSet<Namespace>();
203 for (String uri : registry.getURIs()) {
204 String prefix = registry.getPrefix(uri);
205 result.add(new BasicNamespace(prefix, uri));
206 }
207 return Collections.unmodifiableSet(result);
208 } catch (RepositoryException e) {
209 throw new NamespaceException(e);
210 } finally {
211 if (session != null) {
212 session.logout();
213 }
214 }
215 }
216
217 }