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

com.phloc.scopes.singleton.SessionApplicationSingleton Maven / Gradle / Ivy

There is a newer version: 6.4.4
Show newest version
/**
 * Copyright (C) 2006-2015 phloc systems
 * http://www.phloc.com
 * office[at]phloc[dot]com
 *
 * 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.phloc.scopes.singleton;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.phloc.commons.annotations.MustImplementEqualsAndHashcode;
import com.phloc.scopes.AbstractSingleton;
import com.phloc.scopes.domain.ISessionApplicationScope;
import com.phloc.scopes.mgr.ScopeManager;

/**
 * This is the base class for singleton objects that reside in the session
 * application non-web scope.
 * 
 * @see com.phloc.scopes.mgr.EScope#SESSION_APPLICATION
 * @author Philip Helger
 */
@MustImplementEqualsAndHashcode
public abstract class SessionApplicationSingleton extends AbstractSingleton implements Serializable
{
  protected SessionApplicationSingleton ()
  {
    super ("getSessionApplicationSingleton");
  }

  private void writeObject (@Nonnull final ObjectOutputStream aOOS) throws IOException
  {
    writeAbstractSingletonFields (aOOS);
  }

  private void readObject (@Nonnull final ObjectInputStream aOIS) throws IOException
  {
    readAbstractSingletonFields (aOIS);
  }

  /**
   * @param bCreateIfNotExisting
   *        true to create a new scope, if none is present yet,
   *        false to return null if either no request
   *        scope or no session scope is present.
   * @return The scope to be used for this type of singleton.
   */
  @Nonnull
  private static ISessionApplicationScope _getStaticScope (final boolean bCreateIfNotExisting)
  {
    return ScopeManager.getSessionApplicationScope (bCreateIfNotExisting);
  }

  /**
   * Get the singleton object in the current session application scope, using
   * the passed class. If the singleton is not yet instantiated, a new instance
   * is created.
   * 
   * @param aClass
   *        The class to be used. May not be null. The class must
   *        be public as needs to have a public no-argument constructor.
   * @return The singleton object and never null.
   */
  @Nonnull
  protected static final  T getSessionApplicationSingleton (@Nonnull final Class  aClass)
  {
    return getSingleton (_getStaticScope (true), aClass);
  }

  /**
   * Get the singleton object if it is already instantiated inside the current
   * session application scope or null if it is not instantiated.
   * 
   * @param aClass
   *        The class to be checked. May not be null.
   * @return The singleton for the specified class is already instantiated,
   *         null otherwise.
   */
  @Nullable
  public static final  T getSessionApplicationSingletonIfInstantiated (@Nonnull final Class  aClass)
  {
    return getSingletonIfInstantiated (_getStaticScope (false), aClass);
  }

  /**
   * Check if a singleton is already instantiated inside the current session
   * application scope
   * 
   * @param aClass
   *        The class to be checked. May not be null.
   * @return true if the singleton for the specified class is
   *         already instantiated, false otherwise.
   */
  public static final boolean isSessionApplicationSingletonInstantiated (@Nonnull final Class  aClass)
  {
    return isSingletonInstantiated (_getStaticScope (false), aClass);
  }

  /**
   * Get all instantiated singleton objects registered in the current session
   * application scope.
   * 
   * @return A non-null list with all instances of this class in
   *         the current session application scope.
   */
  @Nonnull
  public static final List  getAllSessionApplicationSingletons ()
  {
    return getAllSingletons (_getStaticScope (false), SessionApplicationSingleton.class);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy