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 java.security.AccessController;
026    import javax.security.auth.Subject;
027    import javax.security.auth.login.LoginContext;
028    import org.jboss.dna.common.component.ClassLoaderFactory;
029    import org.jboss.dna.common.component.StandardClassLoaderFactory;
030    import org.jboss.dna.common.util.Logger;
031    import org.jboss.dna.graph.properties.NamespaceRegistry;
032    import org.jboss.dna.graph.properties.PropertyFactory;
033    import org.jboss.dna.graph.properties.ValueFactories;
034    import org.jboss.dna.graph.properties.basic.BasicNamespaceRegistry;
035    import org.jboss.dna.graph.properties.basic.BasicPropertyFactory;
036    import org.jboss.dna.graph.properties.basic.StandardValueFactories;
037    
038    /**
039     * @author Randall Hauch
040     * @author John Verhaeg
041     */
042    public class BasicExecutionContext implements ExecutionContext {
043    
044        private final ClassLoaderFactory classLoaderFactory;
045        private final LoginContext loginContext;
046        private final AccessControlContext accessControlContext;
047        private final Subject subject;
048        private final PropertyFactory propertyFactory;
049        private final ValueFactories valueFactories;
050        private final NamespaceRegistry namespaceRegistry;
051    
052        public BasicExecutionContext() {
053            this(null, null, null, null, null);
054        }
055    
056        public BasicExecutionContext( LoginContext loginContext ) {
057            this(loginContext, null, null, null, null);
058        }
059    
060        public BasicExecutionContext( AccessControlContext accessControlContext ) {
061            this(null, accessControlContext, null, null, null);
062        }
063    
064        public BasicExecutionContext( NamespaceRegistry namespaceRegistry,
065                                      ValueFactories valueFactories,
066                                      PropertyFactory propertyFactory ) {
067            this(null, null, namespaceRegistry, valueFactories, propertyFactory);
068        }
069    
070        public BasicExecutionContext( NamespaceRegistry namespaceRegistry ) {
071            this(null, null, namespaceRegistry, null, null);
072        }
073    
074        public BasicExecutionContext( LoginContext loginContext,
075                                      NamespaceRegistry namespaceRegistry,
076                                      ValueFactories valueFactories,
077                                      PropertyFactory propertyFactory ) {
078            this(loginContext, null, namespaceRegistry, valueFactories, propertyFactory);
079        }
080    
081        public BasicExecutionContext( AccessControlContext accessControlContext,
082                                      NamespaceRegistry namespaceRegistry,
083                                      ValueFactories valueFactories,
084                                      PropertyFactory propertyFactory ) {
085            this(null, accessControlContext, namespaceRegistry, valueFactories, propertyFactory);
086        }
087    
088        public BasicExecutionContext( ExecutionContext inheritedContext,
089                                      NamespaceRegistry namespaceRegistry ) {
090            this(inheritedContext.getLoginContext(), inheritedContext.getAccessControlContext(), namespaceRegistry, null, null);
091        }
092    
093        /*
094         * This constructor exists to deal with mutually-exclusive parameters, such as LoginContext and AccessControlContext.
095         */
096        private BasicExecutionContext( LoginContext loginContext,
097                                       AccessControlContext accessControlContext,
098                                       NamespaceRegistry namespaceRegistry,
099                                       ValueFactories valueFactories,
100                                       PropertyFactory propertyFactory ) {
101            this.loginContext = loginContext;
102            this.accessControlContext = accessControlContext;
103            if (loginContext == null) {
104                this.subject = Subject.getSubject(accessControlContext == null ? AccessController.getContext() : accessControlContext);
105            } else {
106                this.subject = loginContext.getSubject();
107            }
108            this.namespaceRegistry = namespaceRegistry == null ? new BasicNamespaceRegistry() : namespaceRegistry;
109            this.valueFactories = valueFactories == null ? new StandardValueFactories(this.namespaceRegistry) : valueFactories;
110            this.propertyFactory = propertyFactory == null ? new BasicPropertyFactory(this.valueFactories) : propertyFactory;
111            this.classLoaderFactory = new StandardClassLoaderFactory();
112        }
113    
114        /**
115         * {@inheritDoc}
116         * 
117         * @see org.jboss.dna.common.component.ClassLoaderFactory#getClassLoader(java.lang.String[])
118         */
119        public ClassLoader getClassLoader( String... classpath ) {
120            return this.classLoaderFactory.getClassLoader(classpath);
121        }
122    
123        /**
124         * {@inheritDoc}
125         * 
126         * @see org.jboss.dna.graph.ExecutionContext#getAccessControlContext()
127         */
128        public AccessControlContext getAccessControlContext() {
129            return accessControlContext;
130        }
131    
132        /**
133         * {@inheritDoc}
134         * 
135         * @see org.jboss.dna.graph.ExecutionContext#getLoginContext()
136         */
137        public LoginContext getLoginContext() {
138            return loginContext;
139        }
140    
141        /**
142         * {@inheritDoc}
143         */
144        public NamespaceRegistry getNamespaceRegistry() {
145            return namespaceRegistry;
146        }
147    
148        /**
149         * {@inheritDoc}
150         */
151        public PropertyFactory getPropertyFactory() {
152            return propertyFactory;
153        }
154    
155        /**
156         * {@inheritDoc}
157         * 
158         * @see org.jboss.dna.graph.ExecutionContext#getSubject()
159         */
160        public Subject getSubject() {
161            return subject;
162        }
163    
164        /**
165         * {@inheritDoc}
166         */
167        public ValueFactories getValueFactories() {
168            return valueFactories;
169        }
170    
171        /**
172         * {@inheritDoc}
173         * 
174         * @see org.jboss.dna.graph.ExecutionContext#getLogger(java.lang.Class)
175         */
176        public Logger getLogger( Class<?> clazz ) {
177            return Logger.getLogger(clazz);
178        }
179    
180        /**
181         * {@inheritDoc}
182         * 
183         * @see org.jboss.dna.graph.ExecutionContext#getLogger(java.lang.String)
184         */
185        public Logger getLogger( String name ) {
186            return Logger.getLogger(name);
187        }
188    }