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