
com.alachisoft.ncache.client.internal.util.DirectoryUtil Maven / Gradle / Ivy
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.alachisoft.ncache.client.internal.util;
import Alachisoft.NCache.Common.AppUtil;
import Alachisoft.NCache.Common.Exceptions.ManagementException;
import Alachisoft.NCache.Config.Dom.CacheServerConfig;
import Alachisoft.NCache.Management.ThinClientConfigManager;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import com.alachisoft.ncache.client.CacheManager;
import com.alachisoft.ncache.runtime.exceptions.SecurityException;
public class DirectoryUtil {
public static CacheServerConfig getCacheDom(String cacheId, String userId, String password, boolean inproc) throws SecurityException {
String filePath = getBaseFilePath("config.ncconf");
CacheServerConfig dom = null;
if (filePath != null)
{
try
{
dom = ThinClientConfigManager.GetConfigDom(cacheId, filePath, userId, password, inproc);
}
catch (ManagementException exception)
{
}
}
return dom;
}
public static boolean searchGlobalDirectory(String directoryName, boolean createNew, tangible.OutObject path)
{
String ncacheInstallDirectory = AppUtil.getInstallDir();
if (ncacheInstallDirectory == null)
return false;
path.argValue = new File(ncacheInstallDirectory, directoryName).getPath();
Path filepath = Paths.get(path.argValue);
if (Files.isDirectory(filepath, LinkOption.NOFOLLOW_LINKS))
{
if (createNew)
{
try
{
(new File(path.argValue)).mkdirs();
return true;
}
catch (RuntimeException e)
{
throw e;
}
}
return true;
}
return false;
}
public static String getBaseFilePath(String fileName)
{
Search result;
tangible.OutObject tempOutResult = new tangible.OutObject();
String tempVar = searchLocal(fileName, tempOutResult);
result = tempOutResult.argValue;
return tempVar;
}
public static String getBaseFilePath(String fileName, Search search, tangible.OutObject result)
{
if (search == Search.LocalSearch)
{
return searchLocal(fileName, result);
}
else
{
if (search == Search.LocalConfigSearch)
{
return searchLocalConfig(fileName, result);
}
else
{
return searchGlobal(fileName, result);
}
}
}
private static String searchLocal(String fileName, tangible.OutObject result)
{
result.argValue = Search.LocalSearch;
String path = null;
path=searchWebConfig(fileName,result);
if (path != null && path.equalsIgnoreCase("") == false) {
return path;
}
path = Paths.get("").toAbsolutePath().toString() + File.separatorChar + fileName;
if ((new File(path)).isFile())
{
return path;
}
return searchLocalConfig(fileName, result);
}
private static String searchWebConfig(String fileName, tangible.OutObject result) {
//config is set in session or any other web application
String path=CacheManager.getConfigPath();
if (path != null && path.equalsIgnoreCase("") == false) {
if (!path.endsWith(Character.toString(File.separatorChar))) {
path = path.concat(Character.toString(File.separatorChar));
}
path = path.concat(fileName);
if ((new File(path)).isFile()) {
return path;
}
}
return path;
}
private static String searchLocalConfig(String fileName, tangible.OutObject result) {
result.argValue = Search.LocalConfigSearch;
String path = null;
String roleRootDir = System.getenv("RoleRoot");
if (roleRootDir != null) {
path = roleRootDir + "\\approot\\" + fileName;
if (!(new File(path)).isFile()) {
path = roleRootDir + "\\approot\\bin\\config\\" + fileName;
if ((new File(path)).isFile()) {
return path;
}
} else {
return path;
}
}
return searchGlobal(fileName, result);
}
private static String searchGlobal(String fileName, tangible.OutObject result)
{
result.argValue = Search.GlobalSearch;
tangible.OutObject tempOutDirectoryPath = new tangible.OutObject<>();
if (searchGlobalDirectory("config", false, tempOutDirectoryPath))
{
String directoryPath = tempOutDirectoryPath.argValue;
File file= new File(directoryPath, fileName);
if (file.exists())
{
return file.getPath();
}
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy