use of io.cdap.cdap.api.app.Application in project cdap by cdapio.
the class DefaultArtifactInspector 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 {
Class<?> mainClass = artifactClassLoader.loadClass(mainClassName);
if (!(Application.class.isAssignableFrom(mainClass))) {
// possible for 3rd party plugin artifacts to have the main class set
return builder;
}
Application app = (Application) mainClass.newInstance();
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, getArtifactRequirements(app.getClass())));
} 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 io.cdap.cdap.api.app.Application in project cdap by cdapio.
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() {
// Create the classloader
EntityImpersonator classLoaderImpersonator = new EntityImpersonator(artifactId.toEntityId(), impersonator);
try (CloseableClassLoader classLoader = artifactRepository.createArtifactClassLoader(new ArtifactDescriptor(artifactId.getNamespace().getId(), artifactId.toArtifactId(), artifactLocation), classLoaderImpersonator)) {
SettableFuture<ConfigResponse> result = SettableFuture.create();
Object appMain = classLoader.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, classLoader);
result.set(response);
return result;
} catch (Throwable t) {
return Futures.immediateFailedFuture(t);
}
}
use of io.cdap.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() {
// Create the classloader
EntityImpersonator classLoaderImpersonator = new EntityImpersonator(artifactId.toEntityId(), impersonator);
try (CloseableClassLoader classLoader = artifactRepository.createArtifactClassLoader(new ArtifactDescriptor(artifactId.getNamespace().getId(), artifactId.toArtifactId(), artifactLocation), classLoaderImpersonator)) {
SettableFuture<ConfigResponse> result = SettableFuture.create();
Object appMain = classLoader.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, classLoader);
result.set(response);
return result;
} catch (Throwable t) {
return Futures.immediateFailedFuture(t);
}
}
use of io.cdap.cdap.api.app.Application in project cdap by caskdata.
the class ApplicationLifecycleService method getLatestAppArtifactForUpgrade.
/**
* Finds latest application artifact for given application and current artifact for upgrading application.
* If no artifact found then returns current artifact as the candidate.
*
* @param appId application Id to find latest app artifact for.
* @param currentArtifactId current artifact used by application.
* @param allowedArtifactScopes artifact scopes to search in for finding candidate artifacts.
* @param allowSnapshot whether to consider snapshot version of artifacts or not for upgrade.
* @return {@link ArtifactSummary} for the artifact to be used for upgrade purpose.
* @throws NotFoundException if there is no artifact available for given artifact.
* @throws Exception if there was an exception during finding candidate artifact.
*/
private ArtifactSummary getLatestAppArtifactForUpgrade(ApplicationId appId, ArtifactId currentArtifactId, Set<ArtifactScope> allowedArtifactScopes, boolean allowSnapshot) throws Exception {
List<ArtifactSummary> availableArtifacts = new ArrayList<>();
// At the least, current artifact should be in the set of available artifacts.
availableArtifacts.add(ArtifactSummary.from(currentArtifactId));
// Find candidate artifacts from all scopes we need to consider.
for (ArtifactScope scope : allowedArtifactScopes) {
NamespaceId artifactNamespaceToConsider = ArtifactScope.SYSTEM.equals(scope) ? NamespaceId.SYSTEM : appId.getParent();
List<ArtifactSummary> artifacts;
try {
artifacts = artifactRepository.getArtifactSummaries(artifactNamespaceToConsider, currentArtifactId.getName(), Integer.MAX_VALUE, ArtifactSortOrder.ASC);
} catch (ArtifactNotFoundException e) {
// This can happen if we are looking for candidate artifact in multiple namespace.
continue;
}
for (ArtifactSummary artifactSummary : artifacts) {
ArtifactVersion artifactVersion = new ArtifactVersion(artifactSummary.getVersion());
// Consider if it is a non-snapshot version artifact or it is a snapshot version than allowSnapshot is true.
if ((artifactVersion.isSnapshot() && allowSnapshot) || !artifactVersion.isSnapshot()) {
availableArtifacts.add(artifactSummary);
}
}
}
// Find the artifact with latest version.
Optional<ArtifactSummary> newArtifactCandidate = availableArtifacts.stream().max(Comparator.comparing(artifactSummary -> new ArtifactVersion(artifactSummary.getVersion())));
io.cdap.cdap.proto.id.ArtifactId currentArtifact = new io.cdap.cdap.proto.id.ArtifactId(appId.getNamespace(), currentArtifactId.getName(), currentArtifactId.getVersion().getVersion());
return newArtifactCandidate.orElseThrow(() -> new ArtifactNotFoundException(currentArtifact));
}
use of io.cdap.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, () -> null);
appSpec = configurer.createSpecification("app", "1.0");
Injector injector = Guice.createInjector(new AppFabricTestModule(cConf));
cConf.setInt(TxConstants.Manager.CFG_TX_MAX_TIMEOUT, 60);
serviceRunner = new DistributedServiceProgramRunner(cConf, yConf, null, ClusterMode.ON_PREMISE, null, injector);
workerRunner = new DistributedWorkerProgramRunner(cConf, yConf, null, ClusterMode.ON_PREMISE, null, injector);
mapreduceRunner = new DistributedMapReduceProgramRunner(cConf, yConf, null, ClusterMode.ON_PREMISE, null, injector);
sparkRunner = new DistributedSparkProgramRunner(SparkCompat.SPARK2_2_11, cConf, yConf, null, null, ClusterMode.ON_PREMISE, null, injector);
workflowRunner = new DistributedWorkflowProgramRunner(cConf, yConf, null, ClusterMode.ON_PREMISE, null, null, injector);
}
Aggregations