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.graph.connectors;
023
024 import java.security.AccessControlContext;
025 import javax.security.auth.Subject;
026 import javax.security.auth.callback.CallbackHandler;
027 import javax.security.auth.login.LoginContext;
028 import javax.security.auth.login.LoginException;
029 import org.jboss.dna.graph.ExecutionContext;
030 import org.jboss.dna.graph.ExecutionContextFactory;
031 import org.jboss.dna.graph.properties.NamespaceRegistry;
032 import org.jboss.dna.graph.properties.basic.BasicNamespaceRegistry;
033
034 /**
035 * Basic implementation of a {@link ExecutionContextFactory} that returns {@link BasicExecutionContext basic}
036 * {@link ExecutionContext}s.
037 *
038 * @author Randall Hauch
039 */
040 public class BasicExecutionContextFactory implements ExecutionContextFactory {
041
042 private final NamespaceRegistry defaultNamespaces = new BasicNamespaceRegistry();
043
044 /**
045 * Create a new instance of this factory.
046 */
047 public BasicExecutionContextFactory() {
048 defaultNamespaces.register("jcr", "http://www.jcp.org/jcr/1.0");
049 defaultNamespaces.register("mix", "http://www.jcp.org/jcr/mix/1.0");
050 defaultNamespaces.register("nt", "http://www.jcp.org/jcr/nt/1.0");
051 defaultNamespaces.register("dna", "http://www.jboss.org/dna");
052 defaultNamespaces.register("dnadtd", "http://www.jboss.org/dna/dtd/1.0");
053 defaultNamespaces.register("dnaxml", "http://www.jboss.org/dna/xml/1.0");
054 }
055
056 /**
057 * Create a new instance of this factory.
058 *
059 * @param defaultNamespaceUri the URI of the namespace that should be used with names that have no specified namespace prefix
060 * @throws IllegalArgumentException if the URI is null
061 */
062 public BasicExecutionContextFactory( String defaultNamespaceUri ) {
063 this();
064 defaultNamespaces.register("", defaultNamespaceUri);
065 }
066
067 /**
068 * @return defaultNamespaces
069 */
070 public NamespaceRegistry getDefaultNamespaces() {
071 return defaultNamespaces;
072 }
073
074 /**
075 * {@inheritDoc}
076 *
077 * @see org.jboss.dna.graph.ExecutionContextFactory#create()
078 */
079 public ExecutionContext create() {
080 ExecutionContext context = new BasicExecutionContext();
081 initialize(context);
082 return context;
083 }
084
085 /**
086 * {@inheritDoc}
087 *
088 * @see org.jboss.dna.graph.ExecutionContextFactory#create(java.security.AccessControlContext)
089 */
090 public ExecutionContext create( AccessControlContext accessControlContext ) {
091 ExecutionContext context = new BasicExecutionContext(accessControlContext);
092 initialize(context);
093 return context;
094 }
095
096 /**
097 * {@inheritDoc}
098 *
099 * @see org.jboss.dna.graph.ExecutionContextFactory#create(javax.security.auth.login.LoginContext)
100 */
101 public ExecutionContext create( LoginContext loginContext ) {
102 ExecutionContext context = new BasicExecutionContext(loginContext);
103 initialize(context);
104 return context;
105 }
106
107 /**
108 * {@inheritDoc}
109 *
110 * @see org.jboss.dna.graph.ExecutionContextFactory#create(java.lang.String)
111 */
112 public ExecutionContext create( String name ) throws LoginException {
113 LoginContext loginContext = new LoginContext(name);
114 ExecutionContext context = new BasicExecutionContext(loginContext);
115 initialize(context);
116 return context;
117 }
118
119 /**
120 * {@inheritDoc}
121 *
122 * @see org.jboss.dna.graph.ExecutionContextFactory#create(java.lang.String, javax.security.auth.Subject)
123 */
124 public ExecutionContext create( String name,
125 Subject subject ) throws LoginException {
126 LoginContext loginContext = new LoginContext(name, subject);
127 ExecutionContext context = new BasicExecutionContext(loginContext);
128 initialize(context);
129 return context;
130 }
131
132 /**
133 * {@inheritDoc}
134 *
135 * @see org.jboss.dna.graph.ExecutionContextFactory#create(java.lang.String, javax.security.auth.callback.CallbackHandler)
136 */
137 public ExecutionContext create( String name,
138 CallbackHandler callbackHandler ) throws LoginException {
139 LoginContext loginContext = new LoginContext(name, callbackHandler);
140 ExecutionContext context = new BasicExecutionContext(loginContext);
141 initialize(context);
142 return context;
143 }
144
145 /**
146 * {@inheritDoc}
147 *
148 * @see org.jboss.dna.graph.ExecutionContextFactory#create(java.lang.String, javax.security.auth.Subject,
149 * javax.security.auth.callback.CallbackHandler)
150 */
151 public ExecutionContext create( String name,
152 Subject subject,
153 CallbackHandler callbackHandler ) throws LoginException {
154 LoginContext loginContext = new LoginContext(name, subject, callbackHandler);
155 ExecutionContext context = new BasicExecutionContext(loginContext);
156 initialize(context);
157 return context;
158 }
159
160 protected synchronized void initialize( ExecutionContext context ) {
161 for (String uri : this.defaultNamespaces.getRegisteredNamespaceUris()) {
162 String prefix = this.defaultNamespaces.getPrefixForNamespaceUri(uri, false);
163 context.getNamespaceRegistry().register(prefix, uri);
164 }
165 }
166 }