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