Search in sources :

Example 1 with AppEngineSdk

use of com.intellij.appengine.sdk.AppEngineSdk in project intellij-community by JetBrains.

the class AppEngineSupportProvider method addSupport.

private void addSupport(final Module module, final ModifiableRootModel rootModel, FrameworkSupportModel frameworkSupportModel, String sdkPath, @Nullable PersistenceApi persistenceApi) {
    FacetType<AppEngineFacet, AppEngineFacetConfiguration> facetType = AppEngineFacet.getFacetType();
    AppEngineFacet appEngineFacet = FacetManager.getInstance(module).addFacet(facetType, facetType.getDefaultFacetName(), null);
    AppEngineWebIntegration webIntegration = AppEngineWebIntegration.getInstance();
    webIntegration.registerFrameworkInModel(frameworkSupportModel, appEngineFacet);
    final AppEngineFacetConfiguration facetConfiguration = appEngineFacet.getConfiguration();
    facetConfiguration.setSdkHomePath(sdkPath);
    final AppEngineSdk sdk = appEngineFacet.getSdk();
    final Artifact webArtifact = findOrCreateWebArtifact(appEngineFacet);
    final VirtualFile webDescriptorDir = webIntegration.suggestParentDirectoryForAppEngineWebXml(module, rootModel);
    if (webDescriptorDir != null) {
        VirtualFile descriptor = createFileFromTemplate(AppEngineTemplateGroupDescriptorFactory.APP_ENGINE_WEB_XML_TEMPLATE, webDescriptorDir, AppEngineUtil.APP_ENGINE_WEB_XML_NAME);
        if (descriptor != null) {
            webIntegration.addDescriptor(webArtifact, module.getProject(), descriptor);
        }
    }
    final Project project = module.getProject();
    webIntegration.addDevServerToModuleDependencies(rootModel, sdk);
    final Library apiJar = addProjectLibrary(module, "AppEngine API", sdk.getUserLibraryPaths(), VirtualFile.EMPTY_ARRAY);
    rootModel.addLibraryEntry(apiJar);
    webIntegration.addLibraryToArtifact(apiJar, webArtifact, project);
    if (persistenceApi != null) {
        facetConfiguration.setRunEnhancerOnMake(true);
        facetConfiguration.setPersistenceApi(persistenceApi);
        facetConfiguration.getFilesToEnhance().addAll(AppEngineUtil.getDefaultSourceRootsToEnhance(rootModel));
        try {
            final VirtualFile[] sourceRoots = rootModel.getSourceRoots();
            final VirtualFile sourceRoot;
            if (sourceRoots.length > 0) {
                sourceRoot = sourceRoots[0];
            } else {
                sourceRoot = findOrCreateChildDirectory(rootModel.getContentRoots()[0], "src");
            }
            VirtualFile metaInf = findOrCreateChildDirectory(sourceRoot, "META-INF");
            if (persistenceApi == PersistenceApi.JDO || persistenceApi == PersistenceApi.JDO3) {
                createFileFromTemplate(AppEngineTemplateGroupDescriptorFactory.APP_ENGINE_JDO_CONFIG_TEMPLATE, metaInf, AppEngineUtil.JDO_CONFIG_XML_NAME);
            } else {
                final VirtualFile file = createFileFromTemplate(AppEngineTemplateGroupDescriptorFactory.APP_ENGINE_JPA_CONFIG_TEMPLATE, metaInf, AppEngineUtil.JPA_CONFIG_XML_NAME);
                if (file != null) {
                    webIntegration.setupJpaSupport(module, file);
                }
            }
        } catch (IOException e) {
            LOG.error(e);
        }
        final Library library = addProjectLibrary(module, "AppEngine ORM", Collections.singletonList(sdk.getOrmLibDirectoryPath()), sdk.getOrmLibSources());
        rootModel.addLibraryEntry(library);
        webIntegration.addLibraryToArtifact(library, webArtifact, project);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) Library(com.intellij.openapi.roots.libraries.Library) IOException(java.io.IOException) AppEngineSdk(com.intellij.appengine.sdk.AppEngineSdk) Artifact(com.intellij.packaging.artifacts.Artifact)

Example 2 with AppEngineSdk

use of com.intellij.appengine.sdk.AppEngineSdk in project intellij-community by JetBrains.

the class AppEngineForbiddenCodeInspection method checkFile.

@Override
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull final InspectionManager manager, final boolean isOnTheFly) {
    final Project project = manager.getProject();
    Module module = ModuleUtilCore.findModuleForPsiElement(file);
    final AppEngineFacet appEngineFacet = AppEngineFacet.getAppEngineFacetByModule(module);
    if (appEngineFacet == null) {
        return null;
    }
    final AppEngineSdk appEngineSdk = appEngineFacet.getSdk();
    if (!appEngineSdk.isValid()) {
        return null;
    }
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    final List<ProblemDescriptor> problems = new ArrayList<>();
    file.accept(new JavaRecursiveElementWalkingVisitor() {

        @Override
        public void visitDocComment(PsiDocComment comment) {
        }

        @Override
        public void visitMethod(PsiMethod method) {
            final PsiModifierList modifierList = method.getModifierList();
            if (modifierList.hasModifierProperty(PsiModifier.NATIVE)) {
                if (!isNativeMethodAllowed(method)) {
                    problems.add(manager.createProblemDescriptor(modifierList, "Native methods aren't allowed in App Engine application", isOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
                }
            }
            super.visitMethod(method);
        }

        @Override
        public void visitNewExpression(PsiNewExpression expression) {
            final PsiJavaCodeReferenceElement classReference = expression.getClassReference();
            if (classReference != null) {
                final PsiElement resolved = classReference.resolve();
                if (resolved instanceof PsiClass) {
                    final String qualifiedName = ((PsiClass) resolved).getQualifiedName();
                    if (qualifiedName != null && appEngineSdk.isMethodInBlacklist(qualifiedName, "new")) {
                        final String message = "App Engine application should not create new instances of '" + qualifiedName + "' class";
                        problems.add(manager.createProblemDescriptor(classReference, message, isOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
                    }
                }
            }
            super.visitNewExpression(expression);
        }

        @Override
        public void visitMethodCallExpression(PsiMethodCallExpression expression) {
            final PsiReferenceExpression methodExpression = expression.getMethodExpression();
            final PsiElement element = methodExpression.resolve();
            if (element instanceof PsiMethod) {
                final PsiMethod method = (PsiMethod) element;
                final PsiClass psiClass = method.getContainingClass();
                if (psiClass != null) {
                    final String qualifiedName = psiClass.getQualifiedName();
                    final String methodName = method.getName();
                    if (qualifiedName != null && appEngineSdk.isMethodInBlacklist(qualifiedName, methodName)) {
                        final String message = "AppEngine application should not call '" + StringUtil.getShortName(qualifiedName) + "." + methodName + "' method";
                        problems.add(manager.createProblemDescriptor(methodExpression, message, isOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
                    }
                }
            }
            super.visitMethodCallExpression(expression);
        }

        @Override
        public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
            final PsiElement resolved = reference.resolve();
            if (resolved instanceof PsiClass) {
                final PsiFile psiFile = resolved.getContainingFile();
                if (psiFile != null) {
                    final VirtualFile virtualFile = psiFile.getVirtualFile();
                    if (virtualFile != null && !fileIndex.isInSource(virtualFile)) {
                        final List<OrderEntry> list = fileIndex.getOrderEntriesForFile(virtualFile);
                        for (OrderEntry entry : list) {
                            if (entry instanceof JdkOrderEntry) {
                                final String className = ClassUtil.getJVMClassName((PsiClass) resolved);
                                if (className != null && !appEngineSdk.isClassInWhiteList(className)) {
                                    problems.add(manager.createProblemDescriptor(reference, "Class '" + className + "' is not included in App Engine JRE White List", isOnTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
                                }
                            }
                        }
                    }
                }
            }
            super.visitReferenceElement(reference);
        }
    });
    return problems.toArray(new ProblemDescriptor[problems.size()]);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrayList(java.util.ArrayList) OrderEntry(com.intellij.openapi.roots.OrderEntry) JdkOrderEntry(com.intellij.openapi.roots.JdkOrderEntry) AppEngineFacet(com.intellij.appengine.facet.AppEngineFacet) ArrayList(java.util.ArrayList) List(java.util.List) AppEngineSdk(com.intellij.appengine.sdk.AppEngineSdk) PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) JdkOrderEntry(com.intellij.openapi.roots.JdkOrderEntry) Project(com.intellij.openapi.project.Project) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) Module(com.intellij.openapi.module.Module)

Example 3 with AppEngineSdk

use of com.intellij.appengine.sdk.AppEngineSdk in project intellij-community by JetBrains.

the class AppEngineUploader method createUploader.

@Nullable
public static AppEngineUploader createUploader(@NotNull Project project, @NotNull Artifact artifact, @NotNull AppEngineServerConfiguration configuration, @NotNull ServerRuntimeInstance.DeploymentOperationCallback callback, @NotNull LoggingHandler loggingHandler) {
    final String explodedPath = artifact.getOutputPath();
    if (explodedPath == null) {
        callback.errorOccurred("Output path isn't specified for '" + artifact.getName() + "' artifact");
        return null;
    }
    final AppEngineFacet appEngineFacet = AppEngineUtil.findAppEngineFacet(project, artifact);
    if (appEngineFacet == null) {
        callback.errorOccurred("App Engine facet not found in '" + artifact.getName() + "' artifact");
        return null;
    }
    final AppEngineSdk sdk = appEngineFacet.getSdk();
    if (!sdk.getAppCfgFile().exists()) {
        callback.errorOccurred("Path to App Engine SDK isn't specified correctly in App Engine Facet settings");
        return null;
    }
    PackagingElementResolvingContext context = ArtifactManager.getInstance(project).getResolvingContext();
    VirtualFile descriptorFile = ArtifactUtil.findSourceFileByOutputPath(artifact, "WEB-INF/appengine-web.xml", context);
    final AppEngineWebApp root = AppEngineFacet.getDescriptorRoot(descriptorFile, appEngineFacet.getModule().getProject());
    if (root != null) {
        final GenericDomValue<String> application = root.getApplication();
        if (StringUtil.isEmptyOrSpaces(application.getValue())) {
            final String name = Messages.showInputDialog(project, "<html>Application name is not specified in appengine-web.xml.<br>" + "Enter application name (see your <a href=\"http://appengine.google.com\">AppEngine account</a>):</html>", CommonBundle.getErrorTitle(), null, "", null);
            if (name == null)
                return null;
            final PsiFile file = application.getXmlTag().getContainingFile();
            new WriteCommandAction(project, file) {

                protected void run(@NotNull final Result result) {
                    application.setStringValue(name);
                }
            }.execute();
            final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
            if (document != null) {
                FileDocumentManager.getInstance().saveDocument(document);
            }
        }
    }
    AppEngineAuthData authData = AppEngineAccountDialog.createAuthData(project, configuration);
    if (authData == null)
        return null;
    return new AppEngineUploader(project, artifact, appEngineFacet, sdk, authData, callback, loggingHandler);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) PackagingElementResolvingContext(com.intellij.packaging.elements.PackagingElementResolvingContext) Document(com.intellij.openapi.editor.Document) AppEngineWebApp(com.intellij.appengine.descriptor.dom.AppEngineWebApp) Result(com.intellij.openapi.application.Result) AppEngineFacet(com.intellij.appengine.facet.AppEngineFacet) PsiFile(com.intellij.psi.PsiFile) AppEngineSdk(com.intellij.appengine.sdk.AppEngineSdk) AppEngineAuthData(com.intellij.appengine.cloud.AppEngineAuthData) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

AppEngineSdk (com.intellij.appengine.sdk.AppEngineSdk)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 AppEngineFacet (com.intellij.appengine.facet.AppEngineFacet)2 Project (com.intellij.openapi.project.Project)2 AppEngineAuthData (com.intellij.appengine.cloud.AppEngineAuthData)1 AppEngineWebApp (com.intellij.appengine.descriptor.dom.AppEngineWebApp)1 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Document (com.intellij.openapi.editor.Document)1 Module (com.intellij.openapi.module.Module)1 JdkOrderEntry (com.intellij.openapi.roots.JdkOrderEntry)1 OrderEntry (com.intellij.openapi.roots.OrderEntry)1 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)1 Library (com.intellij.openapi.roots.libraries.Library)1 Artifact (com.intellij.packaging.artifacts.Artifact)1 PackagingElementResolvingContext (com.intellij.packaging.elements.PackagingElementResolvingContext)1 PsiFile (com.intellij.psi.PsiFile)1 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1