Search in sources :

Example 1 with Application

use of co.cask.cdap.api.app.Application in project cdap by caskdata.

the class ScheduleSpecificationCodecTest method testAppConfigurerRoute.

@Test
public void testAppConfigurerRoute() throws Exception {
    Application app = new AbstractApplication() {

        @Override
        public void configure() {
            // intentionally use the deprecated scheduleWorkflow method to for timeSchedule
            // to test TimeSchedule deserialization
            scheduleWorkflow(Schedules.builder("timeSchedule").createTimeSchedule("0 * * * *"), "workflow");
            scheduleWorkflow(Schedules.builder("streamSizeSchedule").createDataSchedule(Schedules.Source.STREAM, "stream", 1), "workflow");
        }
    };
    ApplicationSpecification specification = Specifications.from(app);
    ApplicationSpecificationAdapter gsonAdapater = ApplicationSpecificationAdapter.create(new ReflectionSchemaGenerator());
    String jsonStr = gsonAdapater.toJson(specification);
    ApplicationSpecification deserializedSpec = gsonAdapater.fromJson(jsonStr);
    Assert.assertEquals(new TimeSchedule("timeSchedule", "", "0 * * * *"), deserializedSpec.getSchedules().get("timeSchedule").getSchedule());
    Assert.assertEquals(new StreamSizeSchedule("streamSizeSchedule", "", "stream", 1), deserializedSpec.getSchedules().get("streamSizeSchedule").getSchedule());
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) AbstractApplication(co.cask.cdap.api.app.AbstractApplication) TimeSchedule(co.cask.cdap.internal.schedule.TimeSchedule) ReflectionSchemaGenerator(co.cask.cdap.internal.io.ReflectionSchemaGenerator) StreamSizeSchedule(co.cask.cdap.internal.schedule.StreamSizeSchedule) Application(co.cask.cdap.api.app.Application) AbstractApplication(co.cask.cdap.api.app.AbstractApplication) Test(org.junit.Test)

Example 2 with Application

use of co.cask.cdap.api.app.Application in project cdap by caskdata.

the class IntegrationTestManager method deployApplication.

@Override
@SuppressWarnings("unchecked")
public ApplicationManager deployApplication(NamespaceId namespace, Class<? extends Application> applicationClz, @Nullable Config configObject, File... bundleEmbeddedJars) {
    // See if the application class comes from file or jar.
    // If it's from JAR, no need to trace dependency since it should already be in an application jar.
    URL appClassURL = applicationClz.getClassLoader().getResource(applicationClz.getName().replace('.', '/') + ".class");
    // Should never happen, otherwise the ClassLoader is broken
    Preconditions.checkNotNull(appClassURL, "Cannot find class %s from the classloader", applicationClz);
    String appConfig = "";
    Type configType = Artifacts.getConfigType(applicationClz);
    try {
        if (configObject != null) {
            appConfig = GSON.toJson(configObject);
        } else {
            configObject = (Config) TypeToken.of(configType).getRawType().newInstance();
        }
        // Create and deploy application jar
        File appJarFile = new File(tmpFolder, String.format("%s-1.0.0-SNAPSHOT.jar", applicationClz.getSimpleName()));
        try {
            if ("jar".equals(appClassURL.getProtocol())) {
                copyJarFile(appClassURL, appJarFile);
            } else {
                Location appJar = AppJarHelper.createDeploymentJar(locationFactory, applicationClz, new Manifest(), CLASS_ACCEPTOR, bundleEmbeddedJars);
                Files.copy(Locations.newInputSupplier(appJar), appJarFile);
            }
            applicationClient.deploy(namespace, appJarFile, appConfig);
        } finally {
            if (!appJarFile.delete()) {
                LOG.warn("Failed to delete temporary app jar {}", appJarFile.getAbsolutePath());
            }
        }
        // Extracts application id from the application class
        Application application = applicationClz.newInstance();
        MockAppConfigurer configurer = new MockAppConfigurer(application);
        application.configure(configurer, new DefaultApplicationContext<>(configObject));
        String applicationId = configurer.getName();
        return new RemoteApplicationManager(namespace.app(applicationId), clientConfig, restClient);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : Type(java.lang.reflect.Type) RemoteApplicationManager(co.cask.cdap.test.remote.RemoteApplicationManager) MockAppConfigurer(co.cask.cdap.app.MockAppConfigurer) Manifest(java.util.jar.Manifest) File(java.io.File) Application(co.cask.cdap.api.app.Application) URL(java.net.URL) IOException(java.io.IOException) Location(org.apache.twill.filesystem.Location)

Example 3 with Application

use of co.cask.cdap.api.app.Application in project cdap by caskdata.

the class InMemoryConfigurator method config.

/**
 * Executes the <code>Application.configure</code> within the same JVM.
 * <p>
 * This method could be dangerous and should be used only in standalone mode.
 * </p>
 *
 * @return A instance of {@link ListenableFuture}.
 */
@Override
public ListenableFuture<ConfigResponse> config() {
    SettableFuture<ConfigResponse> result = SettableFuture.create();
    try {
        Object appMain = artifactClassLoader.loadClass(appClassName).newInstance();
        if (!(appMain instanceof Application)) {
            throw new IllegalStateException(String.format("Application main class is of invalid type: %s", appMain.getClass().getName()));
        }
        Application app = (Application) appMain;
        ConfigResponse response = createResponse(app);
        result.set(response);
        return result;
    } catch (Throwable t) {
        return Futures.immediateFailedFuture(t);
    }
}
Also used : ConfigResponse(co.cask.cdap.app.deploy.ConfigResponse) Application(co.cask.cdap.api.app.Application)

Example 4 with Application

use of co.cask.cdap.api.app.Application in project cdap by caskdata.

the class ArtifactInspector method inspectApplications.

private ArtifactClasses.Builder inspectApplications(Id.Artifact artifactId, ArtifactClasses.Builder builder, Location artifactLocation, ClassLoader artifactClassLoader) throws IOException, InvalidArtifactException {
    // right now we force users to include the application main class as an attribute in their manifest,
    // which forces them to have a single application class.
    // in the future, we may want to let users do this or maybe specify a list of classes or
    // a package that will be searched for applications, to allow multiple applications in a single artifact.
    String mainClassName;
    try {
        Manifest manifest = BundleJarUtil.getManifest(artifactLocation);
        if (manifest == null) {
            return builder;
        }
        Attributes manifestAttributes = manifest.getMainAttributes();
        if (manifestAttributes == null) {
            return builder;
        }
        mainClassName = manifestAttributes.getValue(ManifestFields.MAIN_CLASS);
    } catch (ZipException e) {
        throw new InvalidArtifactException(String.format("Couldn't unzip artifact %s, please check it is a valid jar file.", artifactId), e);
    }
    if (mainClassName == null) {
        return builder;
    }
    try {
        Object appMain = artifactClassLoader.loadClass(mainClassName).newInstance();
        if (!(appMain instanceof Application)) {
            // possible for 3rd party plugin artifacts to have the main class set
            return builder;
        }
        Application app = (Application) appMain;
        java.lang.reflect.Type configType;
        // we can deserialize the config into that object. Otherwise it'll just be a Config
        try {
            configType = Artifacts.getConfigType(app.getClass());
        } catch (Exception e) {
            throw new InvalidArtifactException(String.format("Could not resolve config type for Application class %s in artifact %s. " + "The type must extend Config and cannot be parameterized.", mainClassName, artifactId));
        }
        Schema configSchema = configType == Config.class ? null : schemaGenerator.generate(configType);
        builder.addApp(new ApplicationClass(mainClassName, "", configSchema));
    } catch (ClassNotFoundException e) {
        throw new InvalidArtifactException(String.format("Could not find Application main class %s in artifact %s.", mainClassName, artifactId));
    } catch (UnsupportedTypeException e) {
        throw new InvalidArtifactException(String.format("Config for Application %s in artifact %s has an unsupported schema. " + "The type must extend Config and cannot be parameterized.", mainClassName, artifactId));
    } catch (InstantiationException | IllegalAccessException e) {
        throw new InvalidArtifactException(String.format("Could not instantiate Application class %s in artifact %s.", mainClassName, artifactId), e);
    }
    return builder;
}
Also used : Config(co.cask.cdap.api.Config) PluginConfig(co.cask.cdap.api.plugin.PluginConfig) Schema(co.cask.cdap.api.data.schema.Schema) Attributes(java.util.jar.Attributes) ApplicationClass(co.cask.cdap.api.artifact.ApplicationClass) ZipException(java.util.zip.ZipException) Manifest(java.util.jar.Manifest) ZipException(java.util.zip.ZipException) EOFException(java.io.EOFException) UnsupportedTypeException(co.cask.cdap.api.data.schema.UnsupportedTypeException) IOException(java.io.IOException) InvalidArtifactException(co.cask.cdap.common.InvalidArtifactException) UnsupportedTypeException(co.cask.cdap.api.data.schema.UnsupportedTypeException) Application(co.cask.cdap.api.app.Application) InvalidArtifactException(co.cask.cdap.common.InvalidArtifactException)

Example 5 with Application

use of co.cask.cdap.api.app.Application in project cdap by caskdata.

the class DistributedProgramRunnerTxTimeoutTest method setup.

@BeforeClass
public static void setup() {
    Application app = new AppWithAllProgramTypes();
    DefaultAppConfigurer configurer = new DefaultAppConfigurer(Id.Namespace.DEFAULT, new Id.Artifact(Id.Namespace.DEFAULT, "artifact", new ArtifactVersion("0.1")), app);
    app.configure(configurer, new ApplicationContext() {

        @Override
        public Config getConfig() {
            return null;
        }
    });
    appSpec = configurer.createSpecification("app", "1.0");
    // System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(appSpec));
    cConf.setInt(TxConstants.Manager.CFG_TX_MAX_TIMEOUT, 60);
    flowRunner = new DistributedFlowProgramRunner(null, yConf, cConf, null, null, null, null, null);
    serviceRunner = new DistributedServiceProgramRunner(null, yConf, cConf, null, null);
    workerRunner = new DistributedWorkerProgramRunner(null, yConf, cConf, null, null);
    mapreduceRunner = new DistributedMapReduceProgramRunner(null, yConf, cConf, null, null);
    sparkRunner = new DistributedSparkProgramRunner(SparkCompat.SPARK1_2_10, null, yConf, cConf, null, null, null);
    workflowRunner = new DistributedWorkflowProgramRunner(null, yConf, cConf, null, null, null);
}
Also used : DefaultAppConfigurer(co.cask.cdap.app.DefaultAppConfigurer) Config(co.cask.cdap.api.Config) ApplicationContext(co.cask.cdap.api.app.ApplicationContext) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ProgramId(co.cask.cdap.proto.id.ProgramId) Id(co.cask.cdap.common.id.Id) Application(co.cask.cdap.api.app.Application) AbstractApplication(co.cask.cdap.api.app.AbstractApplication) DistributedSparkProgramRunner(co.cask.cdap.app.runtime.spark.distributed.DistributedSparkProgramRunner) BeforeClass(org.junit.BeforeClass)

Aggregations

Application (co.cask.cdap.api.app.Application)6 IOException (java.io.IOException)3 Config (co.cask.cdap.api.Config)2 AbstractApplication (co.cask.cdap.api.app.AbstractApplication)2 MockAppConfigurer (co.cask.cdap.app.MockAppConfigurer)2 Type (java.lang.reflect.Type)2 Manifest (java.util.jar.Manifest)2 ApplicationContext (co.cask.cdap.api.app.ApplicationContext)1 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)1 ApplicationClass (co.cask.cdap.api.artifact.ApplicationClass)1 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)1 ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)1 Schema (co.cask.cdap.api.data.schema.Schema)1 UnsupportedTypeException (co.cask.cdap.api.data.schema.UnsupportedTypeException)1 PluginConfig (co.cask.cdap.api.plugin.PluginConfig)1 DefaultAppConfigurer (co.cask.cdap.app.DefaultAppConfigurer)1 ConfigResponse (co.cask.cdap.app.deploy.ConfigResponse)1 DistributedSparkProgramRunner (co.cask.cdap.app.runtime.spark.distributed.DistributedSparkProgramRunner)1 InvalidArtifactException (co.cask.cdap.common.InvalidArtifactException)1 Id (co.cask.cdap.common.id.Id)1