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