Skip to content

Commit

Permalink
Directory for GF client can be set by the GF_CLIENT_DIR env property
Browse files Browse the repository at this point in the history
- by default it is still in ${user.home}/.gfclient

Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Dec 7, 2022
1 parent f848c94 commit 6639484
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 255 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import com.sun.enterprise.universal.i18n.LocalStringsImpl;
import com.sun.enterprise.util.StringUtils;
import com.sun.enterprise.util.SystemPropertyConstants;

import java.io.BufferedReader;
import java.io.Console;
import java.io.File;
Expand Down Expand Up @@ -105,8 +104,8 @@ private class CLIRemoteAdminCommand extends RemoteRestAdminCommand {

private static final String JSESSIONID = "JSESSIONID";
private static final String COOKIE_HEADER = "Cookie";
private CookieManager cookieManager = null;
private File sessionCache = null;
private CookieManager cookieManager;
private final File sessionCache;
private final ProgressStatusPrinter statusPrinter;

/**
Expand All @@ -123,17 +122,9 @@ public CLIRemoteAdminCommand(String name, String host, int port, boolean secure,
super.setEnableCommandModelCache(false);
}

StringBuilder sessionFilePath = new StringBuilder();

// Store the cache at: $GFCLIENT/cache/{host}_{port}/session
sessionFilePath.append("cache").append(File.separator);
sessionFilePath.append(host).append("_");
sessionFilePath.append(port).append(File.separator);
sessionFilePath.append("session");

sessionCache = new File(AsadminSecurityUtil.getDefaultClientDir(), sessionFilePath.toString());
statusPrinter = new ProgressStatusPrinter(env.debug() || env.trace() || System.console() == null, env.debug() || env.trace(),
logger);
sessionCache = AsadminSecurityUtil.getGfClientSessionFile(host, port);
statusPrinter = new ProgressStatusPrinter(env.debug() || env.trace() || System.console() == null,
env.debug() || env.trace(), logger);
if (!programOpts.isTerse()) {
super.registerListener(CommandProgress.EVENT_PROGRESSSTATUS_CHANGE, statusPrinter);
super.registerListener(CommandProgress.EVENT_PROGRESSSTATUS_STATE, statusPrinter);
Expand Down Expand Up @@ -736,7 +727,7 @@ protected int executeCommand() throws CommandException, CommandValidationExcepti
}
ar = rac.getActionReport();
if (!returnActionReport && !returnOutput) {
if (output.length() > 0) {
if (!output.isEmpty()) {
logger.info(output);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public class RemoteCommand extends CLICommand {
private static final LocalStringsImpl strings = new LocalStringsImpl(RemoteCommand.class);

// return output string rather than printing it
private boolean returnOutput = false;
private boolean returnOutput;
private String output;
private boolean returnAttributes = false;
private boolean returnAttributes;
private Map<String, String> attrs;
private String usage;

Expand All @@ -94,8 +94,8 @@ private class CLIRemoteAdminCommand extends RemoteAdminCommand {

private static final String JSESSIONID = "JSESSIONID";
private static final String COOKIE_HEADER = "Cookie";
private CookieManager cookieManager = null;
private File sessionCache = null;
private CookieManager cookieManager;
private final File sessionCache;

/**
* Construct a new remote command object. The command and arguments are supplied later using the execute method in the
Expand All @@ -104,16 +104,7 @@ private class CLIRemoteAdminCommand extends RemoteAdminCommand {
public CLIRemoteAdminCommand(String name, String host, int port, boolean secure, String user, char[] password, Logger logger,
String authToken) throws CommandException {
super(name, host, port, secure, user, password, logger, getCommandScope(), authToken, true /* prohibitDirectoryUploads */);

StringBuilder sessionFilePath = new StringBuilder();

// Store the cache at: $GFCLIENT/cache/{host}_{port}/session
sessionFilePath.append("cache").append(File.separator);
sessionFilePath.append(host).append("_");
sessionFilePath.append(port).append(File.separator);
sessionFilePath.append("session");

sessionCache = new File(AsadminSecurityUtil.getDefaultClientDir(), sessionFilePath.toString());
sessionCache = AsadminSecurityUtil.getGfClientSessionFile(host, port);
}

@Override
Expand Down
13 changes: 13 additions & 0 deletions nucleus/admin/util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,18 @@
</excludes>
</resource>
</resources>

<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<environmentVariables>
<GF_CLIENT_DIR>${project.build.directory}/gfclientdir</GF_CLIENT_DIR>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@
import com.sun.enterprise.admin.util.AdminLoggerInfo;
import com.sun.enterprise.security.store.AsadminSecurityUtil;
import com.sun.enterprise.util.io.FileUtils;
import java.io.*;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -38,7 +47,7 @@ public class AdminCacheFileStore implements AdminCache {

private static final Logger LOG = AdminLoggerInfo.getLogger();

private final AdminCacheUtils adminCahceUtils = AdminCacheUtils.getInstance();
private final AdminCacheUtils adminCacheUtils = AdminCacheUtils.getInstance();

private AdminCacheFileStore() {
}
Expand All @@ -52,7 +61,7 @@ public <A> A get(String key, Class<A> clazz) {
if (clazz == null) {
throw new IllegalArgumentException("Attribute clazz can not be null.");
}
DataProvider provider = adminCahceUtils.getProvider(clazz);
DataProvider provider = adminCacheUtils.getProvider(clazz);
if (provider == null) {
return null;
}
Expand All @@ -70,43 +79,42 @@ private InputStream getInputStream(String key) throws IOException {
if (key == null || key.isEmpty()) {
throw new IllegalArgumentException("Attribute key must be unempty.");
}
if (!adminCahceUtils.validateKey(key)) {
if (!adminCacheUtils.validateKey(key)) {
throw new IllegalArgumentException("Attribute key must be in form (([-_.a-zA-Z0-9]+/?)+)");
}
File f = getCacheFile(key);
return new BufferedInputStream(new FileInputStream(f));
}

private File getCacheFile(String key) throws IOException {
File dir = AsadminSecurityUtil.getDefaultClientDir();
int idx = key.lastIndexOf('/');
if (idx > 0) {
dir = new File(dir, key.substring(0, idx));

if (!FileUtils.mkdirsMaybe(dir)) {
throw new IOException("Can't create directory: " + dir);
}
key = key.substring(idx + 1);
if (key.isEmpty()) {
key = DEFAULT_FILENAME;
}
if (idx == 0) {
return new File(AsadminSecurityUtil.GF_CLIENT_DIR, key);
}
File dir = new File(AsadminSecurityUtil.GF_CLIENT_DIR, key.substring(0, idx));
if (!FileUtils.mkdirsMaybe(dir)) {
throw new IOException("Can't create directory: " + dir);
}
key = key.substring(idx + 1);
if (key.isEmpty()) {
key = DEFAULT_FILENAME;
}
return new File(dir, key);
}

@Override
public synchronized void put(String key, Object data) {
LOG.log(Level.FINEST, "put(key, data={0})", new Object[] {key, data});
LOG.log(Level.FINEST, "put(key={0}, data={1})", new Object[] {key, data});
if (key == null || key.isEmpty()) {
throw new IllegalArgumentException("Attribute key must be unempty.");
}
if (!adminCahceUtils.validateKey(key)) {
if (!adminCacheUtils.validateKey(key)) {
throw new IllegalArgumentException("Attribute key must be in form (([-_.a-zA-Z0-9]+/?)+)");
}
if (data == null) {
throw new IllegalArgumentException("Attribute data can not be null.");
}
DataProvider provider = adminCahceUtils.getProvider(data.getClass());
DataProvider provider = adminCacheUtils.getProvider(data.getClass());
if (provider == null) {
throw new IllegalStateException("There is no data provider for " + data.getClass());
}
Expand Down Expand Up @@ -145,7 +153,7 @@ public boolean contains(String key) {
if (key == null || key.isEmpty()) {
throw new IllegalArgumentException("Attribute key must be unempty.");
}
if (!adminCahceUtils.validateKey(key)) {
if (!adminCacheUtils.validateKey(key)) {
throw new IllegalArgumentException("Attribute key must be in form (([-_.a-zA-Z0-9]+/?)+)");
}
File cacheFile;
Expand All @@ -162,7 +170,7 @@ public Date lastUpdated(String key) {
if (key == null || key.isEmpty()) {
throw new IllegalArgumentException("Attribute key must be unempty.");
}
if (!adminCahceUtils.validateKey(key)) {
if (!adminCacheUtils.validateKey(key)) {
throw new IllegalArgumentException("Attribute key must be in form (([-_.a-zA-Z0-9]+/?)+)");
}
File cacheFile;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -17,8 +18,13 @@
package com.sun.enterprise.admin.util.cache;

import com.sun.enterprise.util.io.FileUtils;
import java.io.*;
import java.nio.charset.Charset;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

import org.jvnet.hk2.annotations.Service;

/**
Expand All @@ -29,16 +35,6 @@
@Service
public class StringDataProvider implements DataProvider {

private Charset charset;

public StringDataProvider() {
try {
charset = Charset.forName("UTF-8");
} catch (Exception ex) {
charset = Charset.defaultCharset();
}
}

@Override
public boolean accept(Class clazz) {
return String.class.isAssignableFrom(clazz);
Expand All @@ -47,14 +43,14 @@ public boolean accept(Class clazz) {
@Override
public void writeToStream(Object o, OutputStream stream) throws IOException {
String str = (String) o;
stream.write(str.getBytes(charset));
stream.write(str.getBytes(StandardCharsets.UTF_8));
}

@Override
public Object toInstance(InputStream stream, Class clazz) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileUtils.copy(stream, baos, 0);
return new String(baos.toByteArray(), charset);
return new String(baos.toByteArray(), StandardCharsets.UTF_8);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -51,7 +51,7 @@ public AdminCacheTstBase(final AdminCache cache) {
@BeforeAll
public static void setUpClass() throws Exception {
// Clean up temp directory
final File dir = new File(AsadminSecurityUtil.getDefaultClientDir(), TEST_CACHE_COTEXT);
final File dir = new File(AsadminSecurityUtil.GF_CLIENT_DIR, TEST_CACHE_COTEXT);
recursiveDelete(dir);
// Test to create and write data
if (!dir.mkdirs()) {
Expand All @@ -70,7 +70,7 @@ public static void setUpClass() throws Exception {

@AfterAll
public static void tearDownClass() throws Exception {
recursiveDelete(new File(AsadminSecurityUtil.getDefaultClientDir(), TEST_CACHE_COTEXT));
recursiveDelete(new File(AsadminSecurityUtil.GF_CLIENT_DIR, TEST_CACHE_COTEXT));
}


Expand All @@ -80,7 +80,7 @@ public void testPutGet() {
final String qeen1 = "Crazy Little Thing Called Love";
final String qeen1Key = TEST_CACHE_COTEXT + "Qeen1";
cache.put(qeen1Key, qeen1);
File f = new File(AsadminSecurityUtil.getDefaultClientDir(), qeen1Key);
File f = new File(AsadminSecurityUtil.GF_CLIENT_DIR, qeen1Key);
assertTrue(f.exists());
assertTrue(f.isFile());
String str = cache.get(qeen1Key, String.class);
Expand All @@ -91,7 +91,7 @@ public void testPutGet() {
final String qeen2 = "You\'re My Best Friend";
final String qeen2Key = TEST_CACHE_COTEXT + "A-Night-at-the-Opera/Qeen2";
cache.put(qeen2Key, qeen2);
f = new File(AsadminSecurityUtil.getDefaultClientDir(), qeen2Key);
f = new File(AsadminSecurityUtil.GF_CLIENT_DIR, qeen2Key);
assertTrue(f.exists());
assertTrue(f.isFile());
str = cache.get(qeen2Key, String.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -41,7 +41,7 @@ public void testWithFileDelete() {
getCache().put(floyd1Key, floyd1);
final String holder = getCache().get(floyd1Key, String.class); // to be sure that it stay in memory
assertEquals(floyd1, holder);
recursiveDelete(new File(AsadminSecurityUtil.getDefaultClientDir(), TEST_CACHE_COTEXT));
recursiveDelete(new File(AsadminSecurityUtil.GF_CLIENT_DIR, TEST_CACHE_COTEXT));
assertEquals(floyd1, getCache().get(floyd1Key, String.class));
}
}
Loading

0 comments on commit 6639484

Please sign in to comment.