All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hcl.domino.jna.runtime.JNADominoRuntime Maven / Gradle / Ivy

The newest version!
/*
 * ==========================================================================
 * Copyright (C) 2019-2022 HCL America, Inc. ( http://www.hcl.com/ )
 *                            All rights reserved.
 * ==========================================================================
 * Licensed under the  Apache License, Version 2.0  (the "License").  You may
 * not use this file except in compliance with the License.  You may obtain a
 * copy of the License at .
 *
 * Unless  required  by applicable  law or  agreed  to  in writing,  software
 * distributed under the License is distributed on an  "AS IS" BASIS, WITHOUT
 * WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the  specific language  governing permissions  and limitations
 * under the License.
 * ==========================================================================
 */
package com.hcl.domino.jna.runtime;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Optional;

import com.hcl.domino.commons.util.NotesErrorUtils;
import com.hcl.domino.commons.util.StringUtil;
import com.hcl.domino.jna.JNADominoClient;
import com.hcl.domino.jna.internal.DisposableMemory;
import com.hcl.domino.jna.internal.NotesStringUtils;
import com.hcl.domino.jna.internal.capi.NotesCAPI;
import com.hcl.domino.misc.DominoEnumUtil;
import com.hcl.domino.misc.NotesConstants;
import com.hcl.domino.runtime.DominoRuntime;
import com.hcl.domino.runtime.NSDMode;
import com.sun.jna.Memory;

public class JNADominoRuntime implements DominoRuntime {
	private JNADominoClient m_parent;
	
	public JNADominoRuntime(JNADominoClient parent) {
		m_parent = parent;
	}

	public JNADominoClient getParent() {
		return m_parent;
	}
	
	@Override
	public Optional getDataDirectory() {
		try(DisposableMemory retPathName = new DisposableMemory(NotesConstants.MAXPATH)) {
			NotesCAPI.get().OSGetDataDirectory(retPathName);
			NotesCAPI.get().OSPathAddTrailingPathSep(retPathName);
			String pathAsStr = NotesStringUtils.fromLMBCS(retPathName, -1);
			if(StringUtil.isNotEmpty(pathAsStr)) {
				return Optional.of(Paths.get(pathAsStr));
			} else {
				return Optional.empty();
			}
		}
	}

	@Override
	public Optional getProgramDirectory() {
		try(DisposableMemory retPathName = new DisposableMemory(NotesConstants.MAXPATH)) {
			NotesCAPI.get().OSGetExecutableDirectory(retPathName);
			String pathAsStr = NotesStringUtils.fromLMBCS(retPathName, -1);
			if(StringUtil.isNotEmpty(pathAsStr)) {
				return Optional.of(Paths.get(pathAsStr));
			} else {
				return Optional.empty();
			}
		}
	}

	@Override
	public Optional getTempDirectory() {
		try(DisposableMemory retPathName = new DisposableMemory(NotesConstants.MAXPATH)) {
			NotesCAPI.get().OSGetSystemTempDirectory(retPathName, NotesConstants.MAXPATH);
			String pathAsStr = NotesStringUtils.fromLMBCS(retPathName, -1);
			if(StringUtil.isNotEmpty(pathAsStr)) {
				return Optional.of(Paths.get(pathAsStr));
			} else {
				return Optional.empty();
			}
		}
	}

	@Override
	public Optional getViewRebuildDirectory() {
		try(DisposableMemory retPathName = new DisposableMemory(NotesConstants.MAXPATH)) {
	    retPathName.setByte(0, (byte)0);
			NotesCAPI.get().NIFGetViewRebuildDir(retPathName, NotesConstants.MAXPATH);
			String pathAsStr = NotesStringUtils.fromLMBCS(retPathName, -1);
			if(StringUtil.isNotEmpty(pathAsStr)) {
				return Optional.of(Paths.get(pathAsStr));
			} else {
				return Optional.empty();
			}
		}
	}
	
	@Override
	public Optional getSharedDataDirectory() {
    try(DisposableMemory retPathName = new DisposableMemory(NotesConstants.MAXPATH)) {
      NotesCAPI.get().OSGetSharedDataDirectory(retPathName);
      NotesCAPI.get().OSPathAddTrailingPathSep(retPathName);
      String pathAsStr = NotesStringUtils.fromLMBCS(retPathName, -1);
      if(StringUtil.isNotEmpty(pathAsStr)) {
        return Optional.of(Paths.get(pathAsStr));
      } else {
        return Optional.empty();
      }
    }
	}

	@Override
	public void setProperty(String propertyName, String value) {
		Memory variableNameMem = NotesStringUtils.toLMBCS(propertyName, true);
		Memory valueMem = NotesStringUtils.toLMBCS(value, true);
		
		boolean isSoft = true; // allow writing OSGI_HTTP_DYNAMIC_BUNDLES
		NotesCAPI.get().OSSetEnvironmentVariableExt(variableNameMem, valueMem, isSoft ? (short) 1 : (short) 0);
	}

	@Override
	public void setProperty(String propertyName, int value) {
		Memory variableNameMem = NotesStringUtils.toLMBCS(propertyName, true);
		NotesCAPI.get().OSSetEnvironmentInt(variableNameMem, value);
	}

	@Override
	public String getPropertyString(String propertyName) {
		Memory variableNameMem = NotesStringUtils.toLMBCS(propertyName, true);
		try(DisposableMemory rethValueBuffer = new DisposableMemory(NotesConstants.MAXENVVALUE)) {
			short result = NotesCAPI.get().OSGetEnvironmentString(variableNameMem, rethValueBuffer, NotesConstants.MAXENVVALUE);
			if (result==1) {
				String str = NotesStringUtils.fromLMBCS(rethValueBuffer, -1);
				return str;
			}
			else {
				return ""; //$NON-NLS-1$
			}
		}
	}

	@Override
	public int getPropertyInt(String propertyName) {
		String value = getPropertyString(propertyName);
		if (StringUtil.isEmpty(value)) {
			return 0;
		}
		
		try {
			int iVal = (int) Double.parseDouble(value);
			return iVal;
		}
		catch (NumberFormatException e) {
			return 0;
		}
	}

	@Override
	public void invokeNSD(Collection modes) {
		short modesBitmask = DominoEnumUtil.toBitField(NSDMode.class, modes);

		Memory serverNameMem = NotesStringUtils.toLMBCS("", true); //$NON-NLS-1$
		
		short result = NotesCAPI.get().OSRunNSDExt(serverNameMem, modesBitmask);
		NotesErrorUtils.checkResult(result);
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy