com.alachisoft.ncache.client.internal.util.DirectoryUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ncache-professional-client Show documentation
Show all versions of ncache-professional-client Show documentation
NCache Professional client for java.
/*
* 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.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 = Paths.get("").toAbsolutePath().toString() + File.separatorChar + fileName;
if ((new File(path)).isFile())
{
return path;
}
return searchLocalConfig(fileName, result);
}
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;
}
}