All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.opensymphony.xwork2.inject.Scope Maven / Gradle / Ivy

Go to download

XWork is an command-pattern framework that is used to power WebWork as well as other applications. XWork provides an Inversion of Control container, a powerful expression language, data type conversion, validation, and pluggable configuration.

There is a newer version: 2.1.3
Show newest version
/**
 * Copyright (C) 2006 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.opensymphony.xwork2.inject;

import java.util.concurrent.Callable;

/**
 * Scope of an injected objects.
 *
 * @author crazybob
 */
public enum Scope {

  /**
   * One instance per injection.
   */
  DEFAULT {
    @Override
             InternalFactory scopeFactory(Class type, String name,
        InternalFactory factory) {
      return factory;
    }
  },

  /**
   * One instance per container.
   */
  SINGLETON {
    @Override
             InternalFactory scopeFactory(Class type, String name,
        final InternalFactory factory) {
      return new InternalFactory() {
        T instance;
        public T create(InternalContext context) {
          synchronized (context.getContainer()) {
            if (instance == null) {
              instance = factory.create(context);
            }
            return instance;
          }
        }

        @Override
        public String toString() {
          return factory.toString();
        }
      };
    }
  },

  /**
   * One instance per thread.
   *
   * 

Note: if a thread local object strongly references its {@link * Container}, neither the {@code Container} nor the object will be * eligible for garbage collection, i.e. memory leak. */ THREAD { @Override InternalFactory scopeFactory(Class type, String name, final InternalFactory factory) { return new InternalFactory() { final ThreadLocal threadLocal = new ThreadLocal(); public T create(final InternalContext context) { T t = threadLocal.get(); if (t == null) { t = factory.create(context); threadLocal.set(t); } return t; } @Override public String toString() { return factory.toString(); } }; } }, /** * One instance per request. */ REQUEST { @Override InternalFactory scopeFactory(final Class type, final String name, final InternalFactory factory) { return new InternalFactory() { public T create(InternalContext context) { Strategy strategy = context.getScopeStrategy(); try { return strategy.findInRequest( type, name, toCallable(context, factory)); } catch (Exception e) { throw new RuntimeException(e); } } @Override public String toString() { return factory.toString(); } }; } }, /** * One instance per session. */ SESSION { @Override InternalFactory scopeFactory(final Class type, final String name, final InternalFactory factory) { return new InternalFactory() { public T create(InternalContext context) { Strategy strategy = context.getScopeStrategy(); try { return strategy.findInSession( type, name, toCallable(context, factory)); } catch (Exception e) { throw new RuntimeException(e); } } @Override public String toString() { return factory.toString(); } }; } }, /** * One instance per wizard. */ WIZARD { @Override InternalFactory scopeFactory(final Class type, final String name, final InternalFactory factory) { return new InternalFactory() { public T create(InternalContext context) { Strategy strategy = context.getScopeStrategy(); try { return strategy.findInWizard( type, name, toCallable(context, factory)); } catch (Exception e) { throw new RuntimeException(e); } } @Override public String toString() { return factory.toString(); } }; } }; Callable toCallable(final InternalContext context, final InternalFactory factory) { return new Callable() { public T call() throws Exception { return factory.create(context); } }; } /** * Wraps factory with scoping logic. */ abstract InternalFactory scopeFactory( Class type, String name, InternalFactory factory); /** * Pluggable scoping strategy. Enables users to provide custom * implementations of request, session, and wizard scopes. Implement and * pass to {@link * Container#setScopeStrategy(com.opensymphony.xwork2.inject.Scope.Strategy)}. */ public interface Strategy { /** * Finds an object for the given type and name in the request scope. * Creates a new object if necessary using the given factory. */ T findInRequest(Class type, String name, Callable factory) throws Exception; /** * Finds an object for the given type and name in the session scope. * Creates a new object if necessary using the given factory. */ T findInSession(Class type, String name, Callable factory) throws Exception; /** * Finds an object for the given type and name in the wizard scope. * Creates a new object if necessary using the given factory. */ T findInWizard(Class type, String name, Callable factory) throws Exception; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy