Package net.ajiaojr.spadger.server.entity

Source Code of net.ajiaojr.spadger.server.entity.SpadgerConfig

/*
* Spadger - an open source discussion forum system.
*
* Copyright (C) 2009 The Spadger Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
*
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package net.ajiaojr.spadger.server.entity;

import java.io.Serializable;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import net.ajiaojr.spadger.server.util.PMF;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;
import com.google.gson.annotations.Expose;

/**
* Spadger system configuration. This configuration is written into the
* underlying data store. Only one unique instance of the configuration is
* allowed.
*
* @author The Spadger Team
*/
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class SpadgerConfig implements Serializable,
    net.ajiaojr.spadger.shared.entity.SpadgerConfig {
  private static final long serialVersionUID = 534805242953319861L;

  private static final Logger LOGGER = LoggerFactory
      .getLogger(SpadgerConfig.class);

  /**
   * Storage key for the unique instance.
   */
  public static final String UNIQUE_INSTANCE_KEY = "SPADGER_CONFIG";

  public static SpadgerConfig getInstance() {
    MemcacheService cache = MemcacheServiceFactory.getMemcacheService();

    // Checks if the config object is cached in memory.
    SpadgerConfig config = (SpadgerConfig) cache
        .get(SpadgerConfig.UNIQUE_INSTANCE_KEY);

    // if the config object is not in memory, go to the data to load it, and
    // then store it in the memory
    if (config == null) {
      LOGGER
          .debug("Unabled to obtain SpadgerConfig instance from cache, trying the datastore instead");

      PersistenceManager pm = PMF.get().getPersistenceManager();
      try {
        config = pm.getObjectById(SpadgerConfig.class,
            SpadgerConfig.UNIQUE_INSTANCE_KEY);

        LOGGER.debug("Placing the SpadgerConfig instance into memcache");

        cache.put(SpadgerConfig.UNIQUE_INSTANCE_KEY, config);
      } catch (JDOObjectNotFoundException e) {
        LOGGER
            .debug("SpadgerConfig instance not found, creating one instead...");

        config = new SpadgerConfig();
        pm.makePersistent(config);
        pm.close();

        LOGGER.info("SpadgerConfig instance created and persisted");
      }
    } else {
      LOGGER.debug("Found SpadgerConfig instance in memcache");
    }
    return config;
  }

  @Expose
  @Persistent
  private boolean readOnly = false;

  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private String key = UNIQUE_INSTANCE_KEY;

  @Expose
  @Persistent
  private String name = "Spadger";

  /**
   * Returns the unique key used to store the system configuration.
   *
   * @return the unique key used to store the system configuration.
   */
  public String getKey() {
    return key;
  }

  /**
   * Returns the name of the system.
   *
   * @return the name of the system.
   */
  @Override
  public String getName() {
    return name;
  }

  /**
   * Returns if the system is set in the read only mode.
   *
   * @return if the system is set in the read only mode.
   */
  @Override
  public boolean isReadOnly() {
    return readOnly;
  }

  /**
   * Sets the unique key used to store the system configuration.
   *
   * @param key
   *          the unique key used to store the system configuration.
   */
  public void setKey(String key) {
    this.key = key;
  }

  /**
   * Sets the name of the system.
   *
   * @param name
   *          the name of the system.
   */
  public void setName(String name) {
    this.name = name;
  }

  /**
   * Sets whether the system should be in read only mode.
   *
   * @param isReadOnly
   *          whether the system should be in read only mode.
   */
  public void setReadOnly(boolean isReadOnly) {
    this.readOnly = isReadOnly;
  }
}
TOP

Related Classes of net.ajiaojr.spadger.server.entity.SpadgerConfig

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.