001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.metamodel.runtimecontext;
021
022import org.apache.isis.applib.DomainObjectContainer;
023import org.apache.isis.core.commons.authentication.AuthenticationSession;
024import org.apache.isis.core.commons.authentication.AuthenticationSessionProvider;
025import org.apache.isis.core.commons.components.ApplicationScopedComponent;
026import org.apache.isis.core.commons.components.Injectable;
027import org.apache.isis.core.metamodel.adapter.DomainObjectServices;
028import org.apache.isis.core.metamodel.adapter.LocalizationProvider;
029import org.apache.isis.core.metamodel.adapter.ObjectDirtier;
030import org.apache.isis.core.metamodel.adapter.ObjectPersistor;
031import org.apache.isis.core.metamodel.adapter.QuerySubmitter;
032import org.apache.isis.core.metamodel.adapter.ServicesProvider;
033import org.apache.isis.core.metamodel.adapter.mgr.AdapterManager;
034import org.apache.isis.core.metamodel.deployment.DeploymentCategory;
035import org.apache.isis.core.metamodel.spec.ObjectInstantiator;
036import org.apache.isis.core.metamodel.spec.SpecificationLoader;
037
038/**
039 * Decouples the metamodel from a runtime.
040 * 
041 */
042public interface RuntimeContext extends Injectable, ApplicationScopedComponent {
043
044    public DeploymentCategory getDeploymentCategory();
045
046    /**
047     * A mechanism for returning the <tt>current</tt>
048     * {@link AuthenticationSession}.
049     * 
050     * <p>
051     * Note that the scope of {@link RuntimeContext} is global, whereas
052     * {@link AuthenticationSession} may change over time.
053     */
054    public AuthenticationSessionProvider getAuthenticationSessionProvider();
055
056    public QuerySubmitter getQuerySubmitter();
057
058    public AdapterManager getAdapterManager();
059
060    public ObjectInstantiator getObjectInstantiator();
061
062    public SpecificationLoader getSpecificationLoader();
063
064    public ServicesProvider getServicesProvider();
065
066    /**
067     * aka the ServicesInjector...
068     */
069    public ServicesInjector getDependencyInjector();
070
071    public ObjectDirtier getObjectDirtier();
072
073    public ObjectPersistor getObjectPersistor();
074
075    public DomainObjectServices getDomainObjectServices();
076
077    public LocalizationProvider getLocalizationProvider();
078
079
080    // ///////////////////////////////////////////
081    // container
082    // ///////////////////////////////////////////
083
084    public void setContainer(DomainObjectContainer container);
085
086    public TransactionState getTransactionState();
087
088    public static enum TransactionState {
089        
090        /**
091         * No transaction exists.
092         */
093        NONE,
094        /**
095         * Started, still in progress.
096         * 
097         * <p>
098         * May {@link IsisTransaction#flush() flush},
099         * {@link IsisTransaction#commit() commit} or
100         * {@link IsisTransaction#abort() abort}.
101         */
102        IN_PROGRESS,
103        /**
104         * Started, but has hit an exception.
105         * 
106         * <p>
107         * May not {@link IsisTransaction#flush()} or
108         * {@link IsisTransaction#commit() commit} (will throw an
109         * {@link IllegalStateException}), but can only
110         * {@link IsisTransaction#abort() abort}.
111         * 
112         * <p>
113         * Similar to <tt>setRollbackOnly</tt> in EJBs.
114         */
115        MUST_ABORT,
116        /**
117         * Completed, having successfully committed.
118         * 
119         * <p>
120         * May not {@link IsisTransaction#flush()} or
121         * {@link IsisTransaction#abort() abort} or
122         * {@link IsisTransaction#commit() commit} (will throw
123         * {@link IllegalStateException}).
124         */
125        COMMITTED,
126        /**
127         * Completed, having aborted.
128         * 
129         * <p>
130         * May not {@link IsisTransaction#flush()},
131         * {@link IsisTransaction#commit() commit} or
132         * {@link IsisTransaction#abort() abort} (will throw
133         * {@link IllegalStateException}).
134         */
135        ABORTED;
136
137        private TransactionState(){}
138
139        /**
140         * Whether it is valid to {@link IsisTransaction#flush() flush} this
141         * {@link IsisTransaction transaction}.
142         */
143        public boolean canFlush() {
144            return this == IN_PROGRESS;
145        }
146
147        /**
148         * Whether it is valid to {@link IsisTransaction#commit() commit} this
149         * {@link IsisTransaction transaction}.
150         */
151        public boolean canCommit() {
152            return this == IN_PROGRESS;
153        }
154
155        /**
156         * Whether it is valid to {@link IsisTransaction#markAsAborted() abort} this
157         * {@link IsisTransaction transaction}.
158         */
159        public boolean canAbort() {
160            return this == IN_PROGRESS || this == MUST_ABORT;
161        }
162
163        /**
164         * Whether the {@link IsisTransaction transaction} is complete (and so a
165         * new one can be started).
166         */
167        public boolean isComplete() {
168            return this == COMMITTED || this == ABORTED;
169        }
170
171        public boolean mustAbort() {
172            return this == MUST_ABORT;
173        }
174    }
175
176}