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());
}
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);
}
}
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);
}
}
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;
}
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);
}
Aggregations