Example #1
0
  /**
   * Force all java source and template compilation.
   *
   * @return success ?
   */
  static boolean preCompile() {
    if (usePrecompiled) {
      if (Play.getFile("precompiled").exists()) {
        classloader.getAllClasses();
        Logger.info("Application is precompiled");
        return true;
      }
      Logger.error("Precompiled classes are missing!!");
      fatalServerErrorOccurred();
      return false;
    }
    try {
      Logger.info("Precompiling ...");
      Thread.currentThread().setContextClassLoader(Play.classloader);
      long start = System.currentTimeMillis();
      classloader.getAllClasses();

      if (Logger.isTraceEnabled()) {
        Logger.trace("%sms to precompile the Java stuff", System.currentTimeMillis() - start);
      }

      if (!lazyLoadTemplates) {
        start = System.currentTimeMillis();
        TemplateLoader.getAllTemplate();

        if (Logger.isTraceEnabled()) {
          Logger.trace("%sms to precompile the templates", System.currentTimeMillis() - start);
        }
      }
      return true;
    } catch (Throwable e) {
      Logger.error(e, "Cannot start in PROD mode with errors");
      fatalServerErrorOccurred();
      return false;
    }
  }
public class Application extends Controller {

  public static final File FILE_FOLDER = Play.getFile("public/pictures/");

  public static final String THUMBNAILS = "thumbnail";
  public static final String FULLSIZE = "fullsize";
  public static final String RESIZED = "resized";

  @Util
  public static List<Gallery> getGalleries() {
    return Gallery.all().fetch();
  }

  @Util
  public static List<Gallery> getPicasaGalleries() {
    return PicasaGallery.all().fetch();
  }

  public static void index() {
    if (Gallery.count() > 0) {
      Gallery defaultGallery = Gallery.all().first();
      showGallery(defaultGallery.id, defaultGallery.name);
    } else {
      render();
    }
  }

  public static void showImage(Long id) {
    Content content = Content.findById(id);
    notFoundIfNull(content);
    response.setContentTypeIfNotSet(content.image.type());
    File file = content.image.getFile();
    notFoundIfNull(file);
    renderBinary(file);
  }

  public static void showGallery(Long id, String name) {
    notFoundIfNull(id);
    Gallery gallery = Gallery.findById(id);
    notFoundIfNull(gallery);

    List<Picture> pictures = gallery.getPictures();
    List<ImageView> images = new ArrayList<ImageView>();
    for (Picture picture : pictures) images.add(picture.toImageView());

    render("Application/gallery.html", images, gallery);
  }

  public static void showPicasaGallery(Long id, String name) {
    notFoundIfNull(id);
    PicasaGallery gallery = PicasaGallery.findById(id);
    notFoundIfNull(gallery);

    PicasawebService service = new PicasawebService("portfolio");
    List<PhotoEntry> photoEntries = Collections.emptyList();
    try {
      java.net.URL feedUrl = new java.net.URL(gallery.getFeedUrl());

      AlbumFeed feed = service.getFeed(feedUrl, AlbumFeed.class);
      photoEntries = feed.getPhotoEntries();
    } catch (MalformedURLException e) {
      Logger.error("Service URL for Picasa is not well formed");
      e.printStackTrace();
    } catch (IOException e) {
      Logger.error("Error I/O while communicating with Picasa Service");
      e.printStackTrace();
    } catch (ServiceException e) {
      Logger.error("Picasa service error");
      e.printStackTrace();
    }

    List<ImageView> images = new ArrayList<ImageView>();
    for (PhotoEntry entry : photoEntries) {
      ImageView image = new ImageView();
      // We take the largest
      image.thumbnail =
          entry.getMediaThumbnails().get(entry.getMediaThumbnails().size() - 1).getUrl();
      image.url = entry.getMediaContents().get(0).getUrl();
      images.add(image);
    }

    render("Application/gallery.html", images, gallery);
  }

  public static void showContents() {
    List<Content> contents = Content.findAll();
    render(contents);
  }

  public static void thumbnail(Long galleryId, Long pictureId) {
    File picture = Picture.getFile(galleryId, pictureId, Application.THUMBNAILS);
    if (!picture.exists()) notFound();
    renderBinary(picture);
  }

  public static void resized(Long galleryId, Long pictureId) {
    File picture = Picture.getFile(galleryId, pictureId, Application.RESIZED);
    if (!picture.exists()) notFound();
    renderBinary(picture);
  }
}
Example #3
0
  /** Load all modules. You can even specify the list using the MODULES environement variable. */
  public static void loadModules() {
    if (System.getenv("MODULES") != null) {
      // Modules path is prepended with a env property
      if (System.getenv("MODULES") != null && System.getenv("MODULES").trim().length() > 0) {
        for (String m :
            System.getenv("MODULES")
                .split(System.getProperty("os.name").startsWith("Windows") ? ";" : ":")) {
          File modulePath = new File(m);
          if (!modulePath.exists() || !modulePath.isDirectory()) {
            Logger.error(
                "Module %s will not be loaded because %s does not exist",
                modulePath.getName(), modulePath.getAbsolutePath());
          } else {
            final String modulePathName = modulePath.getName();
            final String moduleName =
                modulePathName.contains("-")
                    ? modulePathName.substring(0, modulePathName.lastIndexOf("-"))
                    : modulePathName;
            addModule(moduleName, modulePath);
          }
        }
      }
    }

    // Load modules from modules/ directory, but get the order from the dependencies.yml file
    // .listFiles() returns items in an OS dependant sequence, which is bad
    // See #781
    // the yaml parser wants play.version as an environment variable
    System.setProperty("play.version", Play.version);
    DependenciesManager dm = new DependenciesManager(applicationPath, frameworkPath, null);

    File localModules = Play.getFile("modules");
    List<String> modules = new ArrayList<String>();
    if (localModules.exists() && localModules.isDirectory()) {
      try {
        modules = dm.retrieveModules();
      } catch (Exception e) {
        Logger.error(
            "There was a problem parsing "
                + DependenciesManager.MODULE_ORDER_CONF
                + " (module will not be loaded in order of the dependencies.yml)",
            e);
        // Load module without considering the dependencies.yml order
        modules = Arrays.asList(localModules.list());
      }
      for (Iterator<String> iter = modules.iterator(); iter.hasNext(); ) {
        String moduleName = (String) iter.next();

        File module = new File(localModules, moduleName);

        if (moduleName.contains("-")) {
          moduleName = moduleName.substring(0, moduleName.indexOf("-"));
        }

        if (module.isDirectory()) {
          addModule(moduleName, module);
        } else {

          File modulePath = new File(IO.readContentAsString(module).trim());
          if (!modulePath.exists() || !modulePath.isDirectory()) {
            Logger.error(
                "Module %s will not be loaded because %s does not exist",
                moduleName, modulePath.getAbsolutePath());
          } else {
            addModule(moduleName, modulePath);
          }
        }
      }
    }

    // Auto add special modules
    if (Play.runningInTestMode()) {
      addModule("_testrunner", new File(Play.frameworkPath, "modules/testrunner"));
    }

    if (Play.mode == Mode.DEV) {
      addModule("_docviewer", new File(Play.frameworkPath, "modules/docviewer"));
    }
  }