Search in sources :

Example 1 with OsgiClientException

use of org.apache.sling.ide.osgi.OsgiClientException in project sling by apache.

the class JVMDebuggerConnection method connectInDebugMode.

boolean connectInDebugMode(ILaunch launch, IServer iServer, IProgressMonitor monitor) throws CoreException {
    long start = System.currentTimeMillis();
    this.launch = launch;
    boolean success = false;
    IVMConnector connector = null;
    connector = JavaRuntime.getVMConnector(IJavaLaunchConfigurationConstants.ID_SOCKET_ATTACH_VM_CONNECTOR);
    if (connector == null) {
        connector = JavaRuntime.getDefaultVMConnector();
    }
    if (connector == null) {
        throw new CoreException(new Status(IStatus.ERROR, "org.apache.sling.ide.eclipse.wst", "Could not get jvm connctor"));
    }
    ISlingLaunchpadServer launchpadServer = (ISlingLaunchpadServer) iServer.loadAdapter(SlingLaunchpadServer.class, monitor);
    ISlingLaunchpadConfiguration configuration = launchpadServer.getConfiguration();
    int debugPort = configuration.getDebugPort();
    if (debugPort <= 0) {
        throw new CoreException(new Status(IStatus.ERROR, "org.apache.sling.ide.eclipse.wst", "debug port not configured"));
    }
    Map<String, String> connectMap = new HashMap<>();
    connectMap.put("hostname", iServer.getHost());
    connectMap.put("port", String.valueOf(debugPort));
    //			Map argMap = null;//configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_CONNECT_MAP, (Map)null);
    int connectTimeout = JavaRuntime.getPreferences().getInt(JavaRuntime.PREF_CONNECT_TIMEOUT);
    //$NON-NLS-1$
    connectMap.put("timeout", Integer.toString(connectTimeout));
    // set the default source locator if required
    @SuppressWarnings("restriction") ISourceLookupDirector sourceLocator = new JavaSourceLookupDirector();
    sourceLocator.setSourcePathComputer(DebugPlugin.getDefault().getLaunchManager().getSourcePathComputer(//$NON-NLS-1$
    "org.eclipse.jdt.launching.sourceLookup.javaSourcePathComputer"));
    List<IRuntimeClasspathEntry> classpathEntries = new ArrayList<>();
    // 1. add java projects first
    for (IJavaProject javaProject : ProjectHelper.getAllJavaProjects()) {
        classpathEntries.add(JavaRuntime.newProjectRuntimeClasspathEntry(javaProject));
    }
    // 2. add the other modules deployed on server
    // 5/30
    ProgressUtils.advance(monitor, 5);
    // 30 - 5 - 1
    int workTicksForReferences = 24;
    SourceReferenceResolver resolver = Activator.getDefault().getSourceReferenceResolver();
    if (resolver != null && configuration.resolveSourcesInDebugMode()) {
        try {
            List<SourceReference> references = osgiClient.findSourceReferences();
            SubMonitor subMonitor = SubMonitor.convert(monitor, "Resolving source references", workTicksForReferences).setWorkRemaining(references.size());
            for (SourceReference reference : references) {
                try {
                    subMonitor.setTaskName("Resolving source reference: " + reference);
                    IRuntimeClasspathEntry classpathEntry = resolver.resolve(reference);
                    if (classpathEntry != null) {
                        classpathEntries.add(classpathEntry);
                    }
                    ProgressUtils.advance(subMonitor, 1);
                } catch (CoreException e) {
                    // don't fail the debug launch for artifact resolution errors
                    Activator.getDefault().getPluginLogger().warn("Failed resolving source reference", e);
                }
            }
            // 29/30
            subMonitor.done();
        } catch (OsgiClientException e1) {
            throw new CoreException(new Status(Status.ERROR, Activator.PLUGIN_ID, e1.getMessage(), e1));
        }
    } else {
        monitor.worked(workTicksForReferences);
    }
    // 3. add the JRE entry
    classpathEntries.add(JavaRuntime.computeJREEntry(launch.getLaunchConfiguration()));
    IRuntimeClasspathEntry[] resolved = JavaRuntime.resolveSourceLookupPath(classpathEntries.toArray(new IRuntimeClasspathEntry[0]), launch.getLaunchConfiguration());
    sourceLocator.setSourceContainers(JavaRuntime.getSourceContainers(resolved));
    sourceLocator.initializeParticipants();
    launch.setSourceLocator(sourceLocator);
    // connect to remote VM
    try {
        // 30/30
        connector.connect(connectMap, monitor, launch);
        success = true;
        long elapsedMillis = System.currentTimeMillis() - start;
        Activator.getDefault().getPluginLogger().tracePerformance("Debug connection to {0}", elapsedMillis, iServer.getName());
    } catch (Exception e) {
        throw new CoreException(new Status(IStatus.ERROR, "org.apache.sling.ide.eclipse.wst", "could not establish debug connection to " + iServer.getHost() + " : " + debugPort, e));
    }
    return success;
}
Also used : HashMap(java.util.HashMap) JavaSourceLookupDirector(org.eclipse.jdt.internal.launching.JavaSourceLookupDirector) ISourceLookupDirector(org.eclipse.debug.core.sourcelookup.ISourceLookupDirector) ArrayList(java.util.ArrayList) SourceReferenceResolver(org.apache.sling.ide.eclipse.core.launch.SourceReferenceResolver) IVMConnector(org.eclipse.jdt.launching.IVMConnector) SourceReference(org.apache.sling.ide.osgi.SourceReference) OsgiClientException(org.apache.sling.ide.osgi.OsgiClientException) ISlingLaunchpadServer(org.apache.sling.ide.eclipse.core.ISlingLaunchpadServer) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) SubMonitor(org.eclipse.core.runtime.SubMonitor) IRuntimeClasspathEntry(org.eclipse.jdt.launching.IRuntimeClasspathEntry) CoreException(org.eclipse.core.runtime.CoreException) DebugException(org.eclipse.debug.core.DebugException) OsgiClientException(org.apache.sling.ide.osgi.OsgiClientException) IJavaProject(org.eclipse.jdt.core.IJavaProject) CoreException(org.eclipse.core.runtime.CoreException) ISlingLaunchpadConfiguration(org.apache.sling.ide.eclipse.core.ISlingLaunchpadConfiguration) ISlingLaunchpadServer(org.apache.sling.ide.eclipse.core.ISlingLaunchpadServer)

Example 2 with OsgiClientException

use of org.apache.sling.ide.osgi.OsgiClientException in project sling by apache.

the class HttpOsgiClient method findSourceReferences.

@Override
public List<SourceReference> findSourceReferences() throws OsgiClientException {
    GetMethod method = new GetMethod(repositoryInfo.appendPath("system/sling/tooling/sourceReferences.json"));
    HttpClient client = getHttpClient();
    try {
        int result = client.executeMethod(method);
        if (result != HttpStatus.SC_OK) {
            throw new HttpException("Got status code " + result + " for call to " + method.getURI());
        }
        Gson gson = new Gson();
        List<SourceReference> refs = new ArrayList<>();
        try (JsonReader jsonReader = new JsonReader(new InputStreamReader(method.getResponseBodyAsStream(), StandardCharsets.US_ASCII))) {
            jsonReader.beginArray();
            while (jsonReader.hasNext()) {
                if (jsonReader.nextName().equals("sourceReference")) {
                    SourceReferenceFromJson sourceReference = gson.fromJson(jsonReader, SourceReference.class);
                    if (sourceReference.isMavenType()) {
                        refs.add(sourceReference.getMavenSourceReference());
                    }
                } else {
                    jsonReader.skipValue();
                }
            }
            jsonReader.endArray();
            return refs;
        }
    } catch (IOException e) {
        throw new OsgiClientException(e);
    } finally {
        method.releaseConnection();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) IOException(java.io.IOException) SourceReference(org.apache.sling.ide.osgi.SourceReference) OsgiClientException(org.apache.sling.ide.osgi.OsgiClientException) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JsonReader(com.google.gson.stream.JsonReader) HttpException(org.apache.commons.httpclient.HttpException)

Example 3 with OsgiClientException

use of org.apache.sling.ide.osgi.OsgiClientException in project sling by apache.

the class SlingLaunchpadBehaviour method publishBundleModule.

private void publishBundleModule(IModule[] module, IProgressMonitor monitor) throws CoreException {
    final IProject project = module[0].getProject();
    boolean installLocally = getServer().getAttribute(ISlingLaunchpadServer.PROP_INSTALL_LOCALLY, true);
    monitor.beginTask("deploying via local install", 5);
    try {
        OsgiClient osgiClient = Activator.getDefault().getOsgiClientFactory().createOsgiClient(ServerUtil.getRepositoryInfo(getServer(), monitor));
        Version supportBundleVersion = osgiClient.getBundleVersion(EmbeddedArtifactLocator.SUPPORT_BUNDLE_SYMBOLIC_NAME);
        monitor.worked(1);
        if (supportBundleVersion == null) {
            throw new CoreException(new Status(Status.ERROR, Activator.PLUGIN_ID, "The support bundle was not found, please install it via the server properties page."));
        }
        IJavaProject javaProject = ProjectHelper.asJavaProject(project);
        IFolder outputFolder = (IFolder) project.getWorkspace().getRoot().findMember(javaProject.getOutputLocation());
        IPath outputLocation = outputFolder.getLocation();
        //ensure the MANIFEST.MF exists - if it doesn't then let the publish fail with a warn (instead of an error)
        IResource manifest = outputFolder.findMember("META-INF/MANIFEST.MF");
        if (manifest == null) {
            Activator.getDefault().getPluginLogger().warn("Project " + project + " does not have a META-INF/MANIFEST.MF (yet) - not publishing this time");
            Activator.getDefault().issueConsoleLog("InstallBundle", outputFolder.getLocation().toOSString(), "Project " + project + " does not have a META-INF/MANIFEST.MF (yet) - not publishing this time");
            monitor.done();
            setModulePublishState(module, IServer.PUBLISH_STATE_FULL);
            return;
        }
        monitor.worked(1);
        //osgiClient must have a timeout!!!
        if (installLocally) {
            osgiClient.installLocalBundle(outputLocation.toOSString());
            monitor.worked(3);
        } else {
            JarBuilder builder = new JarBuilder();
            InputStream bundle = builder.buildJar(outputFolder);
            monitor.worked(1);
            osgiClient.installLocalBundle(bundle, outputFolder.getLocation().toOSString());
            monitor.worked(2);
        }
        setModulePublishState(module, IServer.PUBLISH_STATE_NONE);
    } catch (URISyntaxException e1) {
        throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e1.getMessage(), e1));
    } catch (OsgiClientException e1) {
        throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed installing bundle : " + e1.getMessage(), e1));
    } finally {
        monitor.done();
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IPath(org.eclipse.core.runtime.IPath) InputStream(java.io.InputStream) URISyntaxException(java.net.URISyntaxException) IProject(org.eclipse.core.resources.IProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) CoreException(org.eclipse.core.runtime.CoreException) Version(org.osgi.framework.Version) OsgiClient(org.apache.sling.ide.osgi.OsgiClient) OsgiClientException(org.apache.sling.ide.osgi.OsgiClientException) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 4 with OsgiClientException

use of org.apache.sling.ide.osgi.OsgiClientException in project sling by apache.

the class SlingLaunchpadBehaviour method start.

public void start(IProgressMonitor monitor) throws CoreException {
    boolean success = false;
    Result<ResourceProxy> result = null;
    monitor = SubMonitor.convert(monitor, "Starting server", 10).setWorkRemaining(50);
    Repository repository;
    RepositoryInfo repositoryInfo;
    OsgiClient client;
    try {
        repository = ServerUtil.connectRepository(getServer(), monitor);
        repositoryInfo = ServerUtil.getRepositoryInfo(getServer(), monitor);
        client = Activator.getDefault().getOsgiClientFactory().createOsgiClient(repositoryInfo);
    } catch (CoreException e) {
        setServerState(IServer.STATE_STOPPED);
        throw e;
    } catch (URISyntaxException e) {
        setServerState(IServer.STATE_STOPPED);
        throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
    }
    // 10/50 done
    monitor.worked(10);
    try {
        EmbeddedArtifactLocator artifactLocator = Activator.getDefault().getArtifactLocator();
        // 15/50 done
        installBundle(monitor, client, artifactLocator.loadSourceSupportBundle(), SUPPORT_SOURCE_BUNDLE_SYMBOLIC_NAME);
        // 20/50 done
        installBundle(monitor, client, artifactLocator.loadToolingSupportBundle(), SUPPORT_BUNDLE_SYMBOLIC_NAME);
    } catch (IOException | OsgiClientException e) {
        Activator.getDefault().getPluginLogger().warn("Failed reading the installation support bundle", e);
    }
    try {
        if (getServer().getMode().equals(ILaunchManager.DEBUG_MODE)) {
            debuggerConnection = new JVMDebuggerConnection(client);
            success = debuggerConnection.connectInDebugMode(launch, getServer(), SubMonitor.convert(monitor, 30));
        // 50/50 done
        } else {
            Command<ResourceProxy> command = repository.newListChildrenNodeCommand("/");
            result = command.execute();
            success = result.isSuccess();
            // 50/50 done
            monitor.worked(30);
        }
        if (success) {
            setServerState(IServer.STATE_STARTED);
        } else {
            setServerState(IServer.STATE_STOPPED);
            String message = "Unable to connect to the Server. Please make sure a server instance is running ";
            if (result != null) {
                message += " (" + result.toString() + ")";
            }
            throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, message));
        }
    } catch (CoreException | RuntimeException e) {
        setServerState(IServer.STATE_STOPPED);
        throw e;
    } finally {
        monitor.done();
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) RepositoryInfo(org.apache.sling.ide.transport.RepositoryInfo) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy) Repository(org.apache.sling.ide.transport.Repository) CoreException(org.eclipse.core.runtime.CoreException) OsgiClient(org.apache.sling.ide.osgi.OsgiClient) EmbeddedArtifactLocator(org.apache.sling.ide.artifacts.EmbeddedArtifactLocator) OsgiClientException(org.apache.sling.ide.osgi.OsgiClientException)

Example 5 with OsgiClientException

use of org.apache.sling.ide.osgi.OsgiClientException in project sling by apache.

the class HttpOsgiClient method installBundle.

@Override
public void installBundle(InputStream in, String fileName) throws OsgiClientException {
    if (in == null) {
        throw new IllegalArgumentException("in may not be null");
    }
    if (fileName == null) {
        throw new IllegalArgumentException("fileName may not be null");
    }
    // append pseudo path after root URL to not get redirected for nothing
    final PostMethod filePost = new PostMethod(repositoryInfo.appendPath("system/console/install"));
    try {
        // set referrer
        filePost.setRequestHeader("referer", "about:blank");
        List<Part> partList = new ArrayList<>();
        partList.add(new StringPart("action", "install"));
        partList.add(new StringPart("_noredir_", "_noredir_"));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        IOUtils.copy(in, baos);
        PartSource partSource = new ByteArrayPartSource(fileName, baos.toByteArray());
        partList.add(new FilePart("bundlefile", partSource));
        partList.add(new StringPart("bundlestart", "start"));
        Part[] parts = partList.toArray(new Part[partList.size()]);
        filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
        int status = getHttpClient().executeMethod(filePost);
        if (status != 200) {
            throw new OsgiClientException("Method execution returned status " + status);
        }
    } catch (IOException e) {
        throw new OsgiClientException(e);
    } finally {
        filePost.releaseConnection();
    }
}
Also used : ByteArrayPartSource(org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource) PartSource(org.apache.commons.httpclient.methods.multipart.PartSource) PostMethod(org.apache.commons.httpclient.methods.PostMethod) ArrayList(java.util.ArrayList) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) ByteArrayPartSource(org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) OsgiClientException(org.apache.sling.ide.osgi.OsgiClientException)

Aggregations

OsgiClientException (org.apache.sling.ide.osgi.OsgiClientException)7 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)3 CoreException (org.eclipse.core.runtime.CoreException)3 IStatus (org.eclipse.core.runtime.IStatus)3 Status (org.eclipse.core.runtime.Status)3 JsonReader (com.google.gson.stream.JsonReader)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 URISyntaxException (java.net.URISyntaxException)2 HashMap (java.util.HashMap)2 HttpClient (org.apache.commons.httpclient.HttpClient)2 HttpException (org.apache.commons.httpclient.HttpException)2 GetMethod (org.apache.commons.httpclient.methods.GetMethod)2 OsgiClient (org.apache.sling.ide.osgi.OsgiClient)2 SourceReference (org.apache.sling.ide.osgi.SourceReference)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 Gson (com.google.gson.Gson)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Reader (java.io.Reader)1