Skip to content

Commit

Permalink
Try-with
Browse files Browse the repository at this point in the history
Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Feb 20, 2024
1 parent d9f4534 commit f6a14a9
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 147 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2009, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,36 +17,37 @@

package com.sun.enterprise.admin.cli.embeddable;

import org.glassfish.admin.payload.PayloadFilesManager;
import org.glassfish.admin.payload.PayloadImpl;
import org.glassfish.api.ActionReport;
import org.glassfish.api.admin.CommandException;
import org.glassfish.api.admin.ParameterMap;
import org.glassfish.api.admin.Payload;
import org.glassfish.embeddable.Deployer;
import org.glassfish.embeddable.GlassFishException;
import org.jvnet.hk2.annotations.ContractsProvided;

import org.jvnet.hk2.annotations.Service;
import org.glassfish.hk2.api.PerLookup;
import org.glassfish.hk2.api.ServiceLocator;
import com.sun.enterprise.util.io.FileUtils;

import jakarta.inject.Inject;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.glassfish.admin.payload.PayloadFilesManager;
import org.glassfish.admin.payload.PayloadImpl;
import org.glassfish.api.ActionReport;
import org.glassfish.api.admin.CommandException;
import org.glassfish.api.admin.ParameterMap;
import org.glassfish.api.admin.Payload;
import org.glassfish.embeddable.Deployer;
import org.glassfish.embeddable.GlassFishException;
import org.glassfish.hk2.api.PerLookup;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.internal.api.InternalSystemAdministrator;
import org.jvnet.hk2.annotations.ContractsProvided;
import org.jvnet.hk2.annotations.Service;

/**
* This is an implementation of {@link Deployer}. Unlike the other EmbeddedDeployer, this deployer uses admin command
Expand Down Expand Up @@ -148,7 +150,7 @@ public Collection<String> getDeployedApplications() throws GlassFishException {
CommandExecutorImpl executer = habitat.getService(CommandExecutorImpl.class);
ActionReport report = executer.executeCommand("list-components");
Properties props = report.getTopMessagePart().getProps();
return new ArrayList<String>(props.stringPropertyNames());
return new ArrayList<>(props.stringPropertyNames());
} catch (Exception e) {
throw new GlassFishException(e);
}
Expand All @@ -168,37 +170,16 @@ private File createFile(InputStream in) throws IOException {
File file;
file = File.createTempFile("app", "tmp");
file.deleteOnExit();
OutputStream out = null;
try {
out = new FileOutputStream(file);
copyStream(in, out);
try (FileOutputStream out = new FileOutputStream(file)) {
FileUtils.copy(in, out);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// ignore
}
}
if (out != null) {
try {
out.close();
} finally {
// ignore
}
in.close();
}
}
return file;
}

private void copyStream(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[4096];
int len;
while ((len = in.read(buf)) >= 0) {
out.write(buf, 0, len);
}
}

/**
* Extract the payload (client side stub jar files) to the directory specified via --retrieve option.
*
Expand All @@ -208,8 +189,6 @@ private void copyStream(InputStream in, OutputStream out) throws IOException {
*/
private void extractPayload(Payload.Outbound outboundPayload, ActionReport actionReport, File retrieveDir) {
File payloadZip = null;
FileOutputStream payloadOutputStream = null;
FileInputStream payloadInputStream = null;
try {
/*
* Add the report to the payload to mimic what the normal
Expand All @@ -228,41 +207,25 @@ private void extractPayload(Payload.Outbound outboundPayload, ActionReport actio
* then reading from that file.
*/
payloadZip = File.createTempFile("appclient", ".zip");
payloadOutputStream = new FileOutputStream(payloadZip);
outboundPayload.writeTo(payloadOutputStream);
payloadOutputStream.flush();
payloadOutputStream.close();

try (FileOutputStream payloadOutputStream = new FileOutputStream(payloadZip)) {
outboundPayload.writeTo(payloadOutputStream);
}
/*
* Use the temp file's contents as the inbound payload to
* correctly process the downloaded files.
*/
final PayloadFilesManager pfm = new PayloadFilesManager.Perm(retrieveDir,
null /* no action report to record extraction results */, logger);
payloadInputStream = new FileInputStream(payloadZip);
final PayloadImpl.Inbound inboundPayload = PayloadImpl.Inbound.newInstance("application/zip", payloadInputStream);
pfm.processParts(inboundPayload); // explodes the payloadZip.
final PayloadFilesManager pfm = new PayloadFilesManager.Perm(retrieveDir, null, logger);
try (FileInputStream payloadInputStream = new FileInputStream(payloadZip)) {
PayloadImpl.Inbound inboundPayload = PayloadImpl.Inbound.newInstance("application/zip", payloadInputStream);
pfm.processParts(inboundPayload); // explodes the payloadZip.
}
} catch (Exception ex) {
// Log error and ignore exception.
logger.log(Level.WARNING, ex.getMessage(), ex);
} finally {
if (payloadOutputStream != null) {
try {
payloadOutputStream.close();
} catch (IOException ioex) {
logger.warning(ioex.getMessage());
}
}
if (payloadInputStream != null) {
try {
payloadInputStream.close();
} catch (IOException ioex) {
logger.warning(ioex.getMessage());
}
}
if (payloadZip != null) {
if (payloadZip.delete() == false) {
logger.log(Level.WARNING, "Cannot delete payload: {0}", payloadZip.toString());
if (!payloadZip.delete()) {
logger.log(Level.WARNING, "Cannot delete payload: {0}", payloadZip);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@

import org.glassfish.admin.payload.PayloadFilesManager;
import org.glassfish.admin.payload.PayloadImpl;
import org.glassfish.admin.payload.PayloadImpl.Inbound;
import org.glassfish.api.admin.AuthenticationException;
import org.glassfish.api.admin.CommandException;
import org.glassfish.api.admin.CommandModel;
Expand Down Expand Up @@ -1111,36 +1112,31 @@ public void prepareConnection(HttpURLConnection urlConnection) {

@Override
public void useConnection(HttpURLConnection urlConnection) throws CommandException, IOException {

Payload.Inbound inboundPayload;
try (InputStream in = urlConnection.getInputStream()) {
String responseContentType = urlConnection.getContentType();
logger.finer("Response Content-Type: " + responseContentType);
inboundPayload = PayloadImpl.Inbound.newInstance(responseContentType, in);
if (inboundPayload == null) {
throw new IOException(strings.get("NoPayloadSupport", responseContentType));
}
InputStream in = urlConnection.getInputStream();
String responseContentType = urlConnection.getContentType();
logger.finer("Response Content-Type: " + responseContentType);
Inbound inboundPayload = PayloadImpl.Inbound.newInstance(responseContentType, in);
if (inboundPayload == null) {
throw new IOException(strings.get("NoPayloadSupport", responseContentType));
}

boolean isReportProcessed = false;
Iterator<Payload.Part> partIt = inboundPayload.parts();
while (partIt.hasNext()) {
/*
* There should be only one part, which should be the
* metadata, but skip any other parts just in case.
*/
if (!isReportProcessed) {
if (isReportProcessed) {
partIt.next(); // just throw it away
} else {
metadataErrors = new StringBuilder();
try (InputStream inputStream = partIt.next().getInputStream()) {
commandModel = parseMetadata(inputStream, metadataErrors);
}
commandModel = parseMetadata(partIt.next().getInputStream(), metadataErrors);
logger.finer("fetchCommandModel: got command opts: " + commandModel);
isReportProcessed = true;
} else {
partIt.next(); // just throw it away
}
}
}

});
if (commandModel == null) {
if (metadataErrors == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,19 +374,15 @@ private File replaceFile(final Payload.Part part) throws IOException {

private void consumePartBody(final Part part) throws FileNotFoundException, IOException {
try (InputStream is = part.getInputStream()) {
byte[] buffer = new byte[1024 * 64];
while (is.read(buffer) != -1) {
}
is.readAllBytes();
}
}

private void processReport(final Payload.Part part) throws Exception {
if (reportHandler != null) {
try (InputStream inputStream = part.getInputStream()) {
reportHandler.handleReport(inputStream);
}
} else {
if (reportHandler == null) {
consumePartBody(part);
} else {
reportHandler.handleReport(part.getInputStream());
}
}

Expand Down

0 comments on commit f6a14a9

Please sign in to comment.