Esempio n. 1
0
  /** Start the application. Recall to restart ! */
  public static synchronized void start() {
    try {

      if (started) {
        stop();
      }

      if (standalonePlayServer) {
        // Can only register shutdown-hook if running as standalone server
        if (!shutdownHookEnabled) {
          // registers shutdown hook - Now there's a good chance that we can notify
          // our plugins that we're going down when some calls ctrl+c or just kills our process..
          shutdownHookEnabled = true;
          Runtime.getRuntime()
              .addShutdownHook(
                  new Thread() {
                    public void run() {
                      Play.stop();
                    }
                  });
        }
      }

      if (mode == Mode.DEV) {
        // Need a new classloader
        classloader = new ApplicationClassloader();
        // Put it in the current context for any code that relies on having it there
        Thread.currentThread().setContextClassLoader(classloader);
        // Reload plugins
        pluginCollection.reloadApplicationPlugins();
      }

      // Reload configuration
      readConfiguration();

      // Configure logs
      String logLevel = configuration.getProperty("application.log", "INFO");
      // only override log-level if Logger was not configured manually
      if (!Logger.configuredManually) {
        Logger.setUp(logLevel);
      }
      Logger.recordCaller =
          Boolean.parseBoolean(configuration.getProperty("application.log.recordCaller", "false"));

      // Locales
      langs =
          new ArrayList<String>(
              Arrays.asList(configuration.getProperty("application.langs", "").split(",")));
      if (langs.size() == 1 && langs.get(0).trim().length() == 0) {
        langs = new ArrayList<String>(16);
      }

      // Clean templates
      TemplateLoader.cleanCompiledCache();

      // SecretKey
      secretKey = configuration.getProperty("application.secret", "").trim();
      if (secretKey.length() == 0) {
        Logger.warn("No secret key defined. Sessions will not be encrypted");
      }

      // Default web encoding
      String _defaultWebEncoding = configuration.getProperty("application.web_encoding");
      if (_defaultWebEncoding != null) {
        Logger.info("Using custom default web encoding: " + _defaultWebEncoding);
        defaultWebEncoding = _defaultWebEncoding;
        // Must update current response also, since the request/response triggering
        // this configuration-loading in dev-mode have already been
        // set up with the previous encoding
        if (Http.Response.current() != null) {
          Http.Response.current().encoding = _defaultWebEncoding;
        }
      }

      // Try to load all classes
      Play.classloader.getAllClasses();

      // Routes
      Router.detectChanges(ctxPath);

      // Cache
      Cache.init();

      // Plugins
      try {
        pluginCollection.onApplicationStart();
      } catch (Exception e) {
        if (Play.mode.isProd()) {
          Logger.error(e, "Can't start in PROD mode with errors");
        }
        if (e instanceof RuntimeException) {
          throw (RuntimeException) e;
        }
        throw new UnexpectedException(e);
      }

      if (firstStart) {
        Logger.info(
            "Application '%s' is now started !", configuration.getProperty("application.name", ""));
        firstStart = false;
      }

      // We made it
      started = true;
      startedAt = System.currentTimeMillis();

      // Plugins
      pluginCollection.afterApplicationStart();

    } catch (PlayException e) {
      started = false;
      try {
        Cache.stop();
      } catch (Exception ignored) {
      }
      throw e;
    } catch (Exception e) {
      started = false;
      try {
        Cache.stop();
      } catch (Exception ignored) {
      }
      throw new UnexpectedException(e);
    }
  }
Esempio n. 2
0
  /**
   * Init the framework
   *
   * @param root The application path
   * @param id The framework id to use
   */
  public static void init(File root, String id) {
    // Simple things
    Play.id = id;
    Play.started = false;
    Play.applicationPath = root;

    // load all play.static of exists
    initStaticStuff();

    guessFrameworkPath();

    // Read the configuration file
    readConfiguration();

    Play.classes = new ApplicationClasses();

    // Configure logs
    Logger.init();
    String logLevel = configuration.getProperty("application.log", "INFO");

    // only override log-level if Logger was not configured manually
    if (!Logger.configuredManually) {
      Logger.setUp(logLevel);
    }
    Logger.recordCaller =
        Boolean.parseBoolean(configuration.getProperty("application.log.recordCaller", "false"));

    Logger.info("Starting %s", root.getAbsolutePath());

    if (configuration.getProperty("play.tmp", "tmp").equals("none")) {
      tmpDir = null;
      Logger.debug("No tmp folder will be used (play.tmp is set to none)");
    } else {
      tmpDir = new File(configuration.getProperty("play.tmp", "tmp"));
      if (!tmpDir.isAbsolute()) {
        tmpDir = new File(applicationPath, tmpDir.getPath());
      }

      if (Logger.isTraceEnabled()) {
        Logger.trace("Using %s as tmp dir", Play.tmpDir);
      }

      if (!tmpDir.exists()) {
        try {
          if (readOnlyTmp) {
            throw new Exception("ReadOnly tmp");
          }
          tmpDir.mkdirs();
        } catch (Throwable e) {
          tmpDir = null;
          Logger.warn("No tmp folder will be used (cannot create the tmp dir)");
        }
      }
    }

    // Mode
    try {
      mode = Mode.valueOf(configuration.getProperty("application.mode", "DEV").toUpperCase());
    } catch (IllegalArgumentException e) {
      Logger.error(
          "Illegal mode '%s', use either prod or dev",
          configuration.getProperty("application.mode"));
      fatalServerErrorOccurred();
    }

    // Force the Production mode if forceProd or precompile is activate
    // Set to the Prod mode must be done before loadModules call
    // as some modules (e.g. DocViewver) is only available in DEV
    if (usePrecompiled || forceProd || System.getProperty("precompile") != null) {
      mode = Mode.PROD;
    }

    // Context path
    ctxPath = configuration.getProperty("http.path", ctxPath);

    // Build basic java source path
    VirtualFile appRoot = VirtualFile.open(applicationPath);
    roots.add(appRoot);
    javaPath = new CopyOnWriteArrayList<VirtualFile>();
    javaPath.add(appRoot.child("app"));
    javaPath.add(appRoot.child("conf"));

    // Build basic templates path
    if (appRoot.child("app/views").exists()) {
      templatesPath = new ArrayList<VirtualFile>(2);
      templatesPath.add(appRoot.child("app/views"));
    } else {
      templatesPath = new ArrayList<VirtualFile>(1);
    }

    // Main route file
    routes = appRoot.child("conf/routes");

    // Plugin route files
    modulesRoutes = new HashMap<String, VirtualFile>(16);

    // Load modules
    loadModules();

    // Load the templates from the framework after the one from the modules
    templatesPath.add(VirtualFile.open(new File(frameworkPath, "framework/templates")));

    // Enable a first classloader
    classloader = new ApplicationClassloader();

    // Fix ctxPath
    if ("/".equals(Play.ctxPath)) {
      Play.ctxPath = "";
    }

    // Default cookie domain
    Http.Cookie.defaultDomain = configuration.getProperty("application.defaultCookieDomain", null);
    if (Http.Cookie.defaultDomain != null) {
      Logger.info("Using default cookie domain: " + Http.Cookie.defaultDomain);
    }

    // Plugins
    pluginCollection.loadPlugins();

    // Done !
    if (mode == Mode.PROD) {
      if (preCompile() && System.getProperty("precompile") == null) {
        start();
      } else {
        return;
      }
    } else {
      Logger.warn("You're running Play! in DEV mode");
    }

    // Plugins
    pluginCollection.onApplicationReady();

    Play.initialized = true;
  }