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 033 /** 034 * @author Randall Hauch 035 */ 036 public class JcrNamespaceRegistry implements NamespaceRegistry { 037 038 private final String repositoryWorkspaceName; 039 private final SessionFactory sessionFactory; 040 041 public JcrNamespaceRegistry( SessionFactory sessionFactory, String repositoryWorkspaceName ) { 042 CheckArg.isNotNull(sessionFactory, "sessionFactory"); 043 CheckArg.isNotNull(repositoryWorkspaceName, "repositoryWorkspaceName"); 044 this.repositoryWorkspaceName = repositoryWorkspaceName; 045 this.sessionFactory = sessionFactory; 046 } 047 048 /** 049 * {@inheritDoc} 050 */ 051 public String getDefaultNamespaceUri() { 052 Session session = null; 053 try { 054 session = this.sessionFactory.createSession(this.repositoryWorkspaceName); 055 return session.getNamespaceURI(""); 056 } catch (RepositoryException e) { 057 throw new NamespaceException(e); 058 } finally { 059 if (session != null) { 060 session.logout(); 061 } 062 } 063 } 064 065 /** 066 * {@inheritDoc} 067 */ 068 public String getNamespaceForPrefix( String prefix ) { 069 Session session = null; 070 try { 071 session = this.sessionFactory.createSession(this.repositoryWorkspaceName); 072 return session.getNamespaceURI(prefix); 073 } catch (RepositoryException e) { 074 throw new NamespaceException(e); 075 } finally { 076 if (session != null) { 077 session.logout(); 078 } 079 } 080 } 081 082 /** 083 * {@inheritDoc} 084 */ 085 public String getPrefixForNamespaceUri( String namespaceUri, boolean generateIfMissing ) { 086 Session session = null; 087 try { 088 session = this.sessionFactory.createSession(this.repositoryWorkspaceName); 089 return session.getNamespacePrefix(namespaceUri); 090 } catch (RepositoryException e) { 091 throw new NamespaceException(e); 092 } finally { 093 if (session != null) { 094 session.logout(); 095 } 096 } 097 } 098 099 /** 100 * {@inheritDoc} 101 */ 102 public boolean isRegisteredNamespaceUri( String namespaceUri ) { 103 Session session = null; 104 try { 105 session = this.sessionFactory.createSession(this.repositoryWorkspaceName); 106 session.getNamespacePrefix(namespaceUri); 107 return true; 108 } catch (javax.jcr.NamespaceException e) { 109 return false; 110 } catch (RepositoryException e) { 111 throw new NamespaceException(e); 112 } finally { 113 if (session != null) { 114 session.logout(); 115 } 116 } 117 } 118 119 /** 120 * {@inheritDoc} 121 */ 122 public String register( String prefix, String namespaceUri ) { 123 String previousNamespaceUriForPrefix = null; 124 Session session = null; 125 try { 126 session = this.sessionFactory.createSession(this.repositoryWorkspaceName); 127 previousNamespaceUriForPrefix = session.getNamespacePrefix(namespaceUri); 128 javax.jcr.NamespaceRegistry registry = session.getWorkspace().getNamespaceRegistry(); 129 registry.registerNamespace(prefix, namespaceUri); 130 } catch (RepositoryException e) { 131 throw new NamespaceException(e); 132 } finally { 133 if (session != null) { 134 session.logout(); 135 } 136 } 137 return previousNamespaceUriForPrefix; 138 } 139 140 /** 141 * {@inheritDoc} 142 */ 143 public Set<String> getRegisteredNamespaceUris() { 144 Session session = null; 145 try { 146 session = this.sessionFactory.createSession(this.repositoryWorkspaceName); 147 javax.jcr.NamespaceRegistry registry = session.getWorkspace().getNamespaceRegistry(); 148 Set<String> result = new HashSet<String>(); 149 for (String uri : registry.getURIs()) { 150 result.add(uri); 151 } 152 return Collections.unmodifiableSet(result); 153 } catch (RepositoryException e) { 154 throw new NamespaceException(e); 155 } finally { 156 if (session != null) { 157 session.logout(); 158 } 159 } 160 } 161 162 }