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

juzu.impl.plugin.asset.AssetMetaModelPlugin Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
/*
 * Copyright 2013 eXo Platform SAS
 *
 * 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 juzu.impl.plugin.asset;

import juzu.impl.common.Name;
import juzu.impl.common.Path;
import juzu.impl.common.Tools;
import juzu.impl.compiler.ElementHandle;
import juzu.impl.plugin.application.metamodel.ApplicationMetaModel;
import juzu.impl.plugin.application.metamodel.ApplicationMetaModelPlugin;
import juzu.impl.metamodel.AnnotationKey;
import juzu.impl.metamodel.AnnotationState;
import juzu.impl.common.JSON;
import juzu.impl.compiler.ProcessingContext;
import juzu.plugin.asset.Assets;

import javax.tools.FileObject;
import javax.tools.StandardLocation;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/** @author Julien Viet */
public class AssetMetaModelPlugin extends ApplicationMetaModelPlugin {

  /** . */
  private static final String[] KINDS = {"scripts","declaredScripts","stylesheets","declaredStylesheets"};

  /** . */
  private HashMap annotations = new HashMap();

  public AssetMetaModelPlugin() {
    super("asset");
  }

  @Override
  public Set> init(ProcessingContext env) {
    return Collections.>singleton(Assets.class);
  }

  @Override
  public void processAnnotationAdded(ApplicationMetaModel metaModel, AnnotationKey key, AnnotationState added) {
    annotations.put(metaModel.getHandle(), added);
  }

  @Override
  public void processAnnotationRemoved(ApplicationMetaModel metaModel, AnnotationKey key, AnnotationState removed) {
    annotations.remove(metaModel.getHandle());
  }

  private List build(List> scripts) {
    List foo = Collections.emptyList();
    if (scripts != null && scripts.size() > 0) {
      foo = new ArrayList(scripts.size());
      for (Map script : scripts) {
        JSON bar = new JSON();
        for (Map.Entry entry : script.entrySet()) {
          bar.set(entry.getKey(), entry.getValue());
        }
        foo.add(bar);
      }
    }
    return foo;
  }

  @Override
  public void prePassivate(ApplicationMetaModel metaModel) {
    AnnotationState annotation = annotations.get(metaModel.getHandle());
    if (annotation != null) {
      String location = (String)annotation.get("location");
      boolean classpath = location == null || "CLASSPATH".equals(location);
      for (String kind : KINDS) {
        List scripts = (List)annotation.get(kind);
        ProcessingContext context = metaModel.getProcessingContext();
        if (scripts != null) {
          for (AnnotationState script : scripts) {
            location = (String)script.get("location");
            if ((location == null && classpath) || "CLASSPATH".equals(location)) {
              String value = (String)script.get("src");
              Path path = Path.parse(value);
              if (path.isRelative()) {
                context.log("Found classpath asset to copy " + value);
                Name qn = metaModel.getHandle().getPackage().append("assets");
                Path.Absolute absolute = qn.resolve(path);
                FileObject src = context.resolveResource(metaModel.getHandle(), absolute);
                if (src != null) {
                  URI srcURI = src.toUri();
                  context.log("Found asset " + absolute + " on source path " + srcURI);
                  InputStream in = null;
                  OutputStream out = null;
                  try {
                    FileObject dst = context.getResource(StandardLocation.CLASS_OUTPUT, absolute);
                    if (dst == null || dst.getLastModified() < src.getLastModified()) {
                      in = src.openInputStream();
                      dst = context.createResource(StandardLocation.CLASS_OUTPUT, absolute, context.get(metaModel.getHandle()));
                      context.log("Copying asset from source path " + srcURI + " to class output " + dst.toUri());
                      out = dst.openOutputStream();
                      Tools.copy(in, out);
                    } else {
                      context.log("Found up to date related asset in class output for " + srcURI);
                    }
                  }
                  catch (IOException e) {
                    context.log("Could not copy asset " + path + " ", e);
                  }
                  finally {
                    Tools.safeClose(in);
                    Tools.safeClose(out);
                  }
                } else {
                  context.log("Could not find asset " + absolute + " on source path");
                }
              }
            }
          }
        }
      }
    }
  }

  @Override
  public JSON getDescriptor(ApplicationMetaModel application) {
    AnnotationState annotation = annotations.get(application.getHandle());
    if (annotation != null) {
      JSON json = new JSON();
      json.set("scripts", build((List>)annotation.get("scripts")));
      json.set("declaredScripts", build((List>)annotation.get("declaredScripts")));
      json.set("stylesheets", build((List>)annotation.get("stylesheets")));
      json.set("declaredStylesheets", build((List>)annotation.get("declaredStylesheets")));
      json.set("package", application.getName().append("assets").toString());
      json.set("location", annotation.get("location"));
      return json;
    } else {
      return null;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy