You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by jg...@apache.org on 2019/05/28 09:46:55 UTC
svn commit: r1860225 [14/16] - in /tomee/deps/branches/commons-daemon: ./
src/ src/assembly/ src/changes/ src/docs/ src/main/ src/main/java/
src/main/java/org/ src/main/java/org/apache/
src/main/java/org/apache/commons/ src/main/java/org/apache/commons...
Added: tomee/deps/branches/commons-daemon/src/native/windows/src/utils.c
URL: http://svn.apache.org/viewvc/tomee/deps/branches/commons-daemon/src/native/windows/src/utils.c?rev=1860225&view=auto
==============================================================================
--- tomee/deps/branches/commons-daemon/src/native/windows/src/utils.c (added)
+++ tomee/deps/branches/commons-daemon/src/native/windows/src/utils.c Tue May 28 09:46:53 2019
@@ -0,0 +1,741 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+
+#include "apxwin.h"
+#include "private.h"
+
+LPWSTR __apxGetEnvironmentVariableW(APXHANDLE hPool, LPCWSTR wsName)
+{
+ LPWSTR wsRet;
+ DWORD rc;
+
+ rc = GetEnvironmentVariableW(wsName, NULL, 0);
+ if (rc == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND)
+ return NULL;
+
+ if (!(wsRet = apxPoolAlloc(hPool, (rc + 1) * sizeof(WCHAR))))
+ return NULL;
+ if (!GetEnvironmentVariableW(wsName, wsRet, rc)) {
+ apxLogWrite(APXLOG_MARK_SYSERR);
+ apxFree(wsRet);
+ return NULL;
+ }
+ return wsRet;
+}
+
+LPSTR __apxGetEnvironmentVariableA(APXHANDLE hPool, LPCSTR szName)
+{
+ LPSTR szRet;
+ DWORD rc;
+
+ rc = GetEnvironmentVariableA(szName, NULL, 0);
+ if (rc == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND)
+ return NULL;
+
+ if (!(szRet = apxPoolAlloc(hPool, rc + 1)))
+ return NULL;
+ if (!GetEnvironmentVariableA(szName, szRet, rc)) {
+ apxLogWrite(APXLOG_MARK_SYSERR);
+ apxFree(szRet);
+ return NULL;
+ }
+ return szRet;
+}
+
+BOOL apxAddToPathW(APXHANDLE hPool, LPCWSTR szAdd)
+{
+ LPWSTR wsAdd;
+ DWORD rc;
+ DWORD al;
+ HMODULE hmodUcrt;
+ WPUTENV wputenv_ucrt;
+
+ rc = GetEnvironmentVariableW(L"PATH", NULL, 0);
+ if (rc == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND)
+ return FALSE;
+ al = 5 + lstrlenW(szAdd) + 1;
+ if (!(wsAdd = apxPoolAlloc(hPool, (al + rc) * sizeof(WCHAR))))
+ return FALSE;
+ lstrcpyW(wsAdd, L"PATH=");
+ lstrcatW(wsAdd, szAdd);
+ lstrcatW(wsAdd, L";");
+ if (GetEnvironmentVariableW(L"PATH", wsAdd + al, rc) != rc - 1) {
+ apxLogWrite(APXLOG_MARK_SYSERR);
+ apxFree(wsAdd);
+ return FALSE;
+ }
+
+ hmodUcrt = LoadLibraryExA("ucrtbase.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
+ if (hmodUcrt != NULL) {
+ wputenv_ucrt = (WPUTENV) GetProcAddress(hmodUcrt, "_wputenv");
+ }
+
+ SetEnvironmentVariableW(L"PATH", wsAdd + 5);
+ _wputenv(wsAdd);
+ if (wputenv_ucrt != NULL) {
+ wputenv_ucrt(wsAdd);
+ }
+
+ apxFree(wsAdd);
+ return TRUE;
+}
+
+LPWSTR AsciiToWide(LPCSTR s, LPWSTR ws)
+{
+ LPWSTR pszSave = ws;
+
+ if (!s) {
+ *ws = L'\0';
+ return pszSave;
+ }
+ do {
+ *ws++ = (WCHAR)*s;
+ } while (*s++);
+ return pszSave;
+}
+
+LPSTR WideToANSI(LPCWSTR ws)
+{
+
+ LPSTR s;
+ int cch = WideCharToMultiByte(CP_ACP, 0, ws, -1, NULL, 0, NULL, NULL);
+ s = (LPSTR)apxAlloc(cch);
+ if (!WideCharToMultiByte(CP_ACP, 0, ws, -1, s, cch, NULL, NULL)) {
+ apxFree(s);
+ return NULL;
+ }
+ return s;
+}
+
+LPWSTR ANSIToWide(LPCSTR cs)
+{
+
+ LPWSTR s;
+ int cch = MultiByteToWideChar(CP_ACP, 0, cs, -1, NULL, 0);
+ s = (LPWSTR)apxAlloc(cch * sizeof(WCHAR));
+ if (!MultiByteToWideChar(CP_ACP, 0, cs, -1, s, cch)) {
+ apxFree(s);
+ return NULL;
+ }
+ return s;
+}
+
+LPSTR MzWideToAscii(LPCWSTR ws, LPSTR s)
+{
+ LPSTR pszSave = s;
+
+ if (ws) {
+ do {
+ *s++ = (CHAR)*ws;
+ ws++;
+ } while( *ws || *(ws + 1));
+ }
+ /* double terminate */
+ *s++ = '\0';
+ *s = '\0';
+ return pszSave;
+}
+
+LPSTR MzWideToANSI(LPCWSTR ws)
+{
+ LPSTR str;
+ LPSTR s;
+ LPCWSTR p = ws;
+ int cch = 0;
+
+ for ( ; p && *p; p++) {
+ int len = WideCharToMultiByte(CP_ACP, 0, p, -1, NULL, 0, NULL, NULL);
+ if (len > 0)
+ cch += len;
+ while (*p)
+ p++;
+ }
+ cch ++;
+ str = s = (LPSTR)apxAlloc(cch + 1);
+
+ p = ws;
+ for ( ; p && *p; p++) {
+ int len = WideCharToMultiByte(CP_ACP, 0, p, -1, s, cch, NULL, NULL);
+ if (len > 0) {
+ s = s + len;
+ cch -= len;
+ }
+ while (*p)
+ p++;
+ }
+ /* double terminate */
+ *s = '\0';
+ return str;
+}
+
+DWORD __apxGetMultiSzLengthA(LPCSTR lpStr, LPDWORD lpdwCount)
+{
+ LPCSTR p = lpStr;
+ if (lpdwCount)
+ *lpdwCount = 0;
+ for ( ; p && *p; p++) {
+ if (lpdwCount)
+ *lpdwCount += 1;
+ while (*p)
+ p++;
+ }
+ return (DWORD)(p - lpStr);
+}
+
+DWORD __apxGetMultiSzLengthW(LPCWSTR lpStr, LPDWORD lpdwCount)
+{
+ LPCWSTR p = lpStr;
+ if (lpdwCount)
+ *lpdwCount = 0;
+ for ( ; p && *p; p++) {
+ if (lpdwCount)
+ *lpdwCount += 1;
+ while (*p)
+ p++;
+ }
+ return (DWORD)((p - lpStr));
+}
+
+LPWSTR apxMultiSzCombine(APXHANDLE hPool, LPCWSTR lpStrA, LPCWSTR lpStrB,
+ LPDWORD lpdwLength)
+{
+ LPWSTR rv;
+ DWORD la = 0, lb = 0;
+ if (!lpStrA && !lpStrB)
+ return NULL; /* Nothing to do if both are NULL */
+
+ la = __apxGetMultiSzLengthW(lpStrA, NULL);
+ lb = __apxGetMultiSzLengthW(lpStrB, NULL);
+
+ rv = apxPoolCalloc(hPool, (la + lb + 1) * sizeof(WCHAR));
+ if (la) {
+ AplMoveMemory(rv, lpStrA, la * sizeof(WCHAR));
+ }
+ if (lb) {
+ AplMoveMemory(&rv[la], lpStrB, lb * sizeof(WCHAR));
+ }
+ if (lpdwLength)
+ *lpdwLength = (la + lb + 1) * sizeof(WCHAR);
+ return rv;
+}
+
+BOOL
+apxSetEnvironmentVariable(APXHANDLE hPool, LPCTSTR szName, LPCTSTR szValue,
+ BOOL bAppend)
+{
+ LPTSTR szNew = (LPTSTR)szValue;
+
+ if (bAppend) {
+ DWORD l = GetEnvironmentVariable(szName, NULL, 0);
+ if (l > 0) {
+ BOOL rv;
+ if (IS_INVALID_HANDLE(hPool))
+ szNew = apxAlloc(l + lstrlen(szValue) + 3);
+ else
+ szNew = apxPoolAlloc(hPool, l + lstrlen(szValue) + 3);
+ GetEnvironmentVariable(szName, szNew, l + 1);
+ lstrcat(szNew, TEXT(";"));
+ lstrcat(szNew, szValue);
+ rv = SetEnvironmentVariable(szName, szNew);
+ apxFree(szNew);
+ return rv;
+ }
+ }
+ return SetEnvironmentVariable(szName, szNew);
+}
+
+
+/** Convert null separated double null terminated string to LPTSTR array)
+ * returns array size
+ */
+DWORD
+apxMultiSzToArrayW(APXHANDLE hPool, LPCWSTR lpString, LPWSTR **lppArray)
+{
+ DWORD i, n, l;
+ char *buff;
+ LPWSTR p;
+
+ l = __apxGetMultiSzLengthW(lpString, &n);
+ if (!n || !l)
+ return 0;
+ if (IS_INVALID_HANDLE(hPool))
+ buff = apxPoolAlloc(hPool, (n + 2) * sizeof(LPWSTR) + (l + 1) * sizeof(WCHAR));
+ else
+ buff = apxAlloc((n + 2) * sizeof(LPWSTR) + (l + 1) * sizeof(WCHAR));
+
+ *lppArray = (LPWSTR *)buff;
+ p = (LPWSTR)(buff + (n + 2) * sizeof(LPWSTR));
+ AplCopyMemory(p, lpString, (l + 1) * sizeof(WCHAR));
+ for (i = 0; i < n; i++) {
+ (*lppArray)[i] = p;
+ while (*p)
+ p++;
+ p++;
+ }
+ (*lppArray)[++i] = NULL;
+
+ return n;
+}
+
+#define QSTR_BOUNDARY 127
+#define QSTR_ALIGN(size) \
+ (((size) + QSTR_BOUNDARY + sizeof(APXMULTISZ) + 2) & ~(QSTR_BOUNDARY))
+
+#define QSTR_SIZE(size) \
+ ((QSTR_ALIGN(size)) - sizeof(APXMULTISZ))
+
+#define QSTR_DATA(q) ((char *)(q) + sizeof(APXMULTISZ))
+
+LPTSTR apxStrCharRemove(LPTSTR szString, TCHAR chSkip)
+{
+ LPTSTR p = szString;
+ LPTSTR q = szString;
+ if (IS_EMPTY_STRING(szString))
+ return szString;
+ while (*p) {
+ if(*p != chSkip)
+ *q++ = *p;
+ ++p;
+ }
+ *q = TEXT('\0');
+
+ return szString;
+}
+
+DWORD apxStrCharRemoveA(LPSTR szString, CHAR chSkip)
+{
+ LPSTR p = szString;
+ LPSTR q = szString;
+ DWORD c = 0;
+ if (IS_EMPTY_STRING(szString))
+ return c;
+ while (*p) {
+ if(*p != chSkip)
+ *q++ = *p;
+ else
+ ++c;
+ ++p;
+ }
+ *q = '\0';
+
+ return c;
+}
+
+DWORD apxStrCharRemoveW(LPWSTR szString, WCHAR chSkip)
+{
+ LPWSTR p = szString;
+ LPWSTR q = szString;
+ DWORD c = 0;
+ if (IS_EMPTY_STRING(szString))
+ return c;
+ while (*p) {
+ if(*p != chSkip)
+ *q++ = *p;
+ else
+ ++c;
+ ++p;
+ }
+ *q = L'\0';
+
+ return c;
+}
+
+void
+apxStrCharReplaceA(LPSTR szString, CHAR chReplace, CHAR chReplaceWith)
+{
+ LPSTR p = szString;
+ LPSTR q = szString;
+
+ if (IS_EMPTY_STRING(szString))
+ return;
+ while (*p) {
+ if(*p == chReplace)
+ *q++ = chReplaceWith;
+ else
+ *q++ = *p;
+ ++p;
+ }
+ *q = '\0';
+}
+
+void
+apxStrCharReplaceW(LPWSTR szString, WCHAR chReplace, WCHAR chReplaceWith)
+{
+ LPWSTR p = szString;
+ LPWSTR q = szString;
+ if (IS_EMPTY_STRING(szString))
+ return;
+ while (*p) {
+ if(*p == chReplace)
+ *q++ = chReplaceWith;
+ else
+ *q++ = *p;
+ ++p;
+ }
+ *q = L'\0';
+}
+
+static const LPCTSTR _st_hex = TEXT("0123456789abcdef");
+#define XTOABUFFER_SIZE (sizeof(ULONG) * 2 + 2)
+#define UTOABUFFER_SIZE (sizeof(ULONG_PTR) * 2 + 2)
+#define LO_NIBLE(x) ((BYTE)((x) & 0x0000000F))
+
+BOOL apxUltohex(ULONG n, LPTSTR lpBuff, DWORD dwBuffLength)
+{
+ LPTSTR p;
+ DWORD i;
+ *lpBuff = 0;
+ if (dwBuffLength < XTOABUFFER_SIZE)
+ return FALSE;
+ p = lpBuff + XTOABUFFER_SIZE;
+ *p-- = 0;
+ for (i = 0; i < sizeof(ULONG) * 2; i++) {
+ *p-- = _st_hex[LO_NIBLE(n)];
+ n = n >> 4;
+ }
+ *p-- = TEXT('x');
+ *p = TEXT('0');
+ return TRUE;
+}
+
+BOOL apxUptohex(ULONG_PTR n, LPTSTR lpBuff, DWORD dwBuffLength)
+{
+ LPTSTR p;
+ DWORD i;
+ *lpBuff = 0;
+ if (dwBuffLength < UTOABUFFER_SIZE)
+ return FALSE;
+ p = lpBuff + UTOABUFFER_SIZE;
+ *p-- = 0;
+ for (i = 0; i < sizeof(ULONG_PTR) * 2; i++) {
+ *p-- = _st_hex[LO_NIBLE(n)];
+ n = n >> 4;
+ }
+ *p-- = TEXT('x');
+ *p = TEXT('0');
+ return TRUE;
+}
+
+ULONG apxAtoulW(LPCWSTR szNum)
+{
+ ULONG rv = 0;
+ DWORD sh = 1;
+ int s = 1;
+ LPCWSTR p = szNum;
+
+ /* go to the last digit */
+ if (!p || !*p)
+ return 0;
+ if (*p == L'-') {
+ s = -1;
+ ++p;
+ }
+ while (*(p + 1)) p++;
+
+ /* go back */
+ while (p >= szNum) {
+ ULONG v = 0;
+ switch (*p--) {
+ case L'0': v = 0UL; break;
+ case L'1': v = 1UL; break;
+ case L'2': v = 2UL; break;
+ case L'3': v = 3UL; break;
+ case L'4': v = 4UL; break;
+ case L'5': v = 5UL; break;
+ case L'6': v = 6UL; break;
+ case L'7': v = 7UL; break;
+ case L'8': v = 8UL; break;
+ case L'9': v = 9UL; break;
+ default:
+ return rv * s;
+ break;
+ }
+ rv = rv + (v * sh);
+ sh = sh * 10;
+ }
+ return rv * s;
+}
+
+/* Make the unique system resource name from prefix and process id
+ *
+ */
+BOOL
+apxMakeResourceName(LPCTSTR szPrefix, LPTSTR lpBuff, DWORD dwBuffLength)
+{
+ DWORD pl = lstrlen(szPrefix);
+ if (dwBuffLength < (pl + 11))
+ return FALSE;
+ lstrcpy(lpBuff, szPrefix);
+ return apxUltohex(GetCurrentProcessId(), lpBuff + pl, dwBuffLength - pl);
+}
+
+INT apxStrMatchW(LPCWSTR szString, LPCWSTR szPattern, BOOL bIgnoreCase)
+{
+ int x, y;
+
+ for (x = 0, y = 0; szPattern[y]; ++y, ++x) {
+ if (!szPattern[x] && (szPattern[y] != L'*' || szPattern[y] != L'?'))
+ return -1;
+ if (szPattern[y] == L'*') {
+ while (szPattern[++y] == L'*');
+ if (!szPattern[y])
+ return 0;
+ while (szString[x]) {
+ INT rc;
+ if ((rc = apxStrMatchW(&szString[x++], &szPattern[y],
+ bIgnoreCase)) != 1)
+ return rc;
+ }
+ return -1;
+ }
+ else if (szPattern[y] != L'?') {
+ if (bIgnoreCase) {
+ if (CharLowerW((LPWSTR)((SIZE_T)szString[x])) !=
+ CharLowerW((LPWSTR)((SIZE_T)szPattern[y])))
+ return 1;
+ }
+ else {
+ if (szString[x] != szPattern[y])
+ return 1;
+ }
+ }
+ }
+ return (szString[x] != L'\0');
+}
+
+INT apxMultiStrMatchW(LPCWSTR szString, LPCWSTR szPattern,
+ WCHAR chSeparator, BOOL bIgnoreCase)
+{
+ WCHAR szM[SIZ_HUGLEN];
+ DWORD i = 0;
+ LPCWSTR p = szPattern;
+ INT m = -1;
+
+ if (chSeparator == 0)
+ return apxStrMatchW(szString, szPattern, bIgnoreCase);
+ while (*p != L'\0') {
+ if (*p == chSeparator) {
+ m = apxStrMatchW(szString, szM, bIgnoreCase);
+ if (m == 0)
+ return 0;
+ p++;
+ i = 0;
+ szM[0] = L'\0';
+ }
+ else {
+ if (i < SIZ_HUGMAX)
+ szM[i++] = *p++;
+ else
+ return -1;
+ }
+ }
+ szM[i] = L'\0';
+ if (szM[0])
+ return apxStrMatchW(szString, szM, bIgnoreCase);
+ else
+ return m;
+}
+
+void apxStrQuoteInplaceW(LPWSTR szString)
+{
+ LPWSTR p = szString;
+ BOOL needsQuote = FALSE;
+ while (*p) {
+ if (*p++ == L' ') {
+ needsQuote = TRUE;
+ break;
+ }
+ }
+ if (needsQuote) {
+ DWORD l = lstrlenW(szString);
+ AplMoveMemory(&szString[1], szString, l * sizeof(WCHAR));
+ szString[0] = L'"';
+ szString[++l] = L'"';
+ szString[++l] = L'\0';
+ }
+}
+
+DWORD apxStrUnQuoteInplaceA(LPSTR szString)
+{
+ LPSTR p = szString;
+ BOOL needsQuote = FALSE;
+ BOOL inQuote = FALSE;
+ while (*p) {
+ if (*p == '"') {
+ if (inQuote)
+ break;
+ else
+ inQuote = TRUE;
+ }
+ else if (*p == ' ') {
+ if (inQuote) {
+ needsQuote = TRUE;
+ break;
+ }
+ }
+ ++p;
+ }
+ if (!needsQuote)
+ return apxStrCharRemoveA(szString, '"');
+ else
+ return 0;
+}
+
+DWORD apxStrUnQuoteInplaceW(LPWSTR szString)
+{
+ LPWSTR p = szString;
+ BOOL needsQuote = FALSE;
+ BOOL inQuote = FALSE;
+ while (*p) {
+ if (*p == L'"') {
+ if (inQuote)
+ break;
+ else
+ inQuote = TRUE;
+ }
+ else if (*p == L' ') {
+ if (inQuote) {
+ needsQuote = TRUE;
+ break;
+ }
+ }
+ ++p;
+ }
+ if (!needsQuote)
+ return apxStrCharRemoveW(szString, L'"');
+ else
+ return 0;
+}
+
+LPWSTR
+apxMszToCRLFW(APXHANDLE hPool, LPCWSTR szStr)
+{
+ DWORD l, c;
+ LPWSTR rv, b;
+ LPCWSTR p = szStr;
+
+ l = __apxGetMultiSzLengthW(szStr, &c);
+ b = rv = apxPoolCalloc(hPool, (l + c + 2) * sizeof(WCHAR));
+
+ while (c > 0) {
+ if (*p)
+ *b++ = *p;
+ else {
+ *b++ = L'\r';
+ *b++ = L'\n';
+ c--;
+ }
+ p++;
+ }
+ return rv;
+}
+
+LPWSTR
+apxCRLFToMszW(APXHANDLE hPool, LPCWSTR szStr, LPDWORD lpdwBytes)
+{
+ DWORD l, c, n = 0;
+ LPWSTR rv, b;
+
+ l = lstrlenW(szStr);
+ b = rv = apxPoolCalloc(hPool, (l + 2) * sizeof(WCHAR));
+ for (c = 0; c < l; c++) {
+ if (szStr[c] == L'\r') {
+ *b++ = '\0';
+ n++;
+ }
+ else if (szStr[c] != L'\n') {
+ *b++ = szStr[c];
+ n++;
+ }
+ }
+ if (lpdwBytes)
+ *lpdwBytes = (n + 2) * sizeof(WCHAR);
+ return rv;
+}
+
+LPWSTR
+apxExpandStrW(APXHANDLE hPool, LPCWSTR szString)
+{
+ LPCWSTR p = szString;
+ while (*p) {
+ if (*p == L'%') {
+ p = szString;
+ break;
+ }
+ ++p;
+ }
+ if (p != szString)
+ return apxPoolStrdupW(hPool, szString);
+ else {
+ DWORD l = ExpandEnvironmentStringsW(szString, NULL, 0);
+ if (l) {
+ LPWSTR rv = apxPoolAlloc(hPool, l * sizeof(WCHAR));
+ l = ExpandEnvironmentStringsW(szString, rv, l);
+ if (l)
+ return rv;
+ else {
+ apxFree(rv);
+ return NULL;
+ }
+ }
+ else
+ return NULL;
+ }
+}
+
+/* To share the semaphores with other processes, we need a NULL ACL
+ * Code from MS KB Q106387
+ */
+PSECURITY_ATTRIBUTES GetNullACL()
+{
+ PSECURITY_DESCRIPTOR pSD;
+ PSECURITY_ATTRIBUTES sa;
+
+ sa = (PSECURITY_ATTRIBUTES) LocalAlloc(LPTR, sizeof(SECURITY_ATTRIBUTES));
+ sa->nLength = sizeof(sizeof(SECURITY_ATTRIBUTES));
+
+ pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
+ sa->lpSecurityDescriptor = pSD;
+
+ if (pSD == NULL || sa == NULL) {
+ return NULL;
+ }
+ SetLastError(0);
+ if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)
+ || GetLastError()) {
+ LocalFree( pSD );
+ LocalFree( sa );
+ return NULL;
+ }
+ if (!SetSecurityDescriptorDacl(pSD, TRUE, (PACL) NULL, FALSE)
+ || GetLastError()) {
+ LocalFree( pSD );
+ LocalFree( sa );
+ return NULL;
+ }
+
+ sa->bInheritHandle = FALSE;
+ return sa;
+}
+
+
+void CleanNullACL(void *sa)
+{
+ if (sa) {
+ LocalFree(((PSECURITY_ATTRIBUTES)sa)->lpSecurityDescriptor);
+ LocalFree(sa);
+ }
+}
Added: tomee/deps/branches/commons-daemon/src/native/windows/xdocs/index.xml
URL: http://svn.apache.org/viewvc/tomee/deps/branches/commons-daemon/src/native/windows/xdocs/index.xml?rev=1860225&view=auto
==============================================================================
--- tomee/deps/branches/commons-daemon/src/native/windows/xdocs/index.xml (added)
+++ tomee/deps/branches/commons-daemon/src/native/windows/xdocs/index.xml Tue May 28 09:46:53 2019
@@ -0,0 +1,33 @@
+<?xml version="1.0"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You 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
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ 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.
+-->
+<document>
+ <properties>
+ <title>Daemon : Procrun</title>
+ <author email="mturk@apache.org">Mladen Turk</author>
+ </properties>
+
+<body>
+<section name="Introduction">
+<p>
+ Procrun is a set of libraries and applications for making Java
+ applications to run on WIN32 much easier.
+</p>
+</section>
+
+</body>
+</document>
Added: tomee/deps/branches/commons-daemon/src/samples/AloneDaemon.sh
URL: http://svn.apache.org/viewvc/tomee/deps/branches/commons-daemon/src/samples/AloneDaemon.sh?rev=1860225&view=auto
==============================================================================
--- tomee/deps/branches/commons-daemon/src/samples/AloneDaemon.sh (added)
+++ tomee/deps/branches/commons-daemon/src/samples/AloneDaemon.sh Tue May 28 09:46:53 2019
@@ -0,0 +1,42 @@
+#!/bin/sh
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# 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.
+#
+# Small shell script to show how to start the sample services.
+# That is for linux, if your are using cygwin look to ServiceDaemon.sh.
+#
+# Adapt the following lines to your configuration
+JAVA_HOME=`echo $JAVA_HOME`
+DAEMON_HOME=`(cd ../..; pwd)`
+USER_HOME=`(cd ../../../..; pwd)`
+TOMCAT_USER=`echo $USER`
+CLASSPATH=\
+$DAEMON_HOME/dist/commons-daemon.jar:\
+$USER_HOME/commons-collections-2.1/commons-collections.jar:\
+$DAEMON_HOME/dist/aloneservice.jar
+
+$DAEMON_HOME/src/native/unix/jsvc \
+ -home $JAVA_HOME \
+ -cp $CLASSPATH \
+ -pidfile ./pidfile \
+ -debug \
+ AloneService
+#
+# To get a verbose JVM
+#-verbose \
+# To get a debug of jsvc.
+#-debug \
+# -user $TOMCAT_USER \
Propchange: tomee/deps/branches/commons-daemon/src/samples/AloneDaemon.sh
------------------------------------------------------------------------------
svn:executable = *
Added: tomee/deps/branches/commons-daemon/src/samples/AloneService.java
URL: http://svn.apache.org/viewvc/tomee/deps/branches/commons-daemon/src/samples/AloneService.java?rev=1860225&view=auto
==============================================================================
--- tomee/deps/branches/commons-daemon/src/samples/AloneService.java (added)
+++ tomee/deps/branches/commons-daemon/src/samples/AloneService.java Tue May 28 09:46:53 2019
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.PrintStream;
+import java.util.Enumeration;
+import java.util.Properties;
+
+/*
+ * That is like the ServiceDaemon but it does not use the Daemon interface.
+ */
+public class AloneService {
+
+ private Properties prop = null;
+ private Process proc[] = null;
+ private ServiceDaemonReadThread readout[] = null;
+ private ServiceDaemonReadThread readerr[] = null;
+
+ @Override
+ protected void finalize() {
+ System.err.println("ServiceDaemon: instance "+this.hashCode()+
+ " garbage collected");
+ }
+
+ /**
+ * init and destroy were added in jakarta-tomcat-daemon.
+ *
+ * @param arguments Unused
+ *
+ * @throws Exception If the daemon cannot be initialised
+ */
+ public void init(String[] arguments) throws Exception {
+ /* Set the err */
+ System.setErr(new PrintStream(new FileOutputStream("/ServiceDaemon.err",true)));
+ System.err.println("ServiceDaemon: instance "+this.hashCode()+
+ " init");
+
+ /* read the properties file */
+ prop = new Properties();
+ try {
+ prop.load(new FileInputStream("startfile"));
+ }
+ catch (Exception e) {
+ // Cannot find startfile.properties.
+ // XXX: Should we print something?
+ }
+ /* create an array to store the processes */
+ int processCount = prop.size();
+ System.err.println("ServiceDaemon: init for " + processCount + " processes");
+ proc = new Process[processCount];
+ readout = new ServiceDaemonReadThread[processCount];
+ readerr = new ServiceDaemonReadThread[processCount];
+ for (int i = 0 ; i < processCount; i++) {
+ proc[i] = null;
+ readout[i] = null;
+ readerr[i] = null;
+ }
+
+ System.err.println("ServiceDaemon: init done ");
+ }
+
+ public void start() {
+ /* Dump a message */
+ System.err.println("ServiceDaemon: starting");
+
+ /* Start */
+ int i=0;
+ for (Enumeration<Object> e = prop.keys(); e.hasMoreElements() ;) {
+ String name = (String) e.nextElement();
+ System.err.println("ServiceDaemon: starting: " + name + " : " + prop.getProperty(name));
+ try {
+ proc[i] = Runtime.getRuntime().exec(prop.getProperty(name));
+ } catch(Exception ex) {
+ System.err.println("Exception: " + ex);
+ }
+ /* Start threads to read from Error and Out streams */
+ readerr[i] =
+ new ServiceDaemonReadThread(proc[i].getErrorStream());
+ readout[i] =
+ new ServiceDaemonReadThread(proc[i].getInputStream());
+ readerr[i].start();
+ readout[i].start();
+ i++;
+ }
+ }
+
+ public void stop() {
+ /* Dump a message */
+ System.err.println("ServiceDaemon: stopping");
+
+ for (int i=0;i<proc.length;i++) {
+ if ( proc[i]==null)
+ continue;
+ proc[i].destroy();
+ try {
+ proc[i].waitFor();
+ } catch(InterruptedException ex) {
+ System.err.println("ServiceDaemon: exception while stopping:" +
+ ex);
+ }
+ }
+
+ System.err.println("ServiceDaemon: stopped");
+ }
+
+ public void destroy() {
+ System.err.println("ServiceDaemon: instance "+this.hashCode()+
+ " destroy");
+ }
+
+}
Added: tomee/deps/branches/commons-daemon/src/samples/Native.c
URL: http://svn.apache.org/viewvc/tomee/deps/branches/commons-daemon/src/samples/Native.c?rev=1860225&view=auto
==============================================================================
--- tomee/deps/branches/commons-daemon/src/samples/Native.c (added)
+++ tomee/deps/branches/commons-daemon/src/samples/Native.c Tue May 28 09:46:53 2019
@@ -0,0 +1,43 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+
+/*
+ * Native routine to core JVM
+ */
+#include <jni.h>
+
+#ifndef _Included_Native
+#define _Included_Native
+#ifdef __cplusplus
+extern "C" {
+#endif
+/*
+ * Class: Native
+ * Method: toto
+ * Signature: ()V
+ */
+JNIEXPORT void JNICALL Java_SimpleDaemon_toto
+ (JNIEnv *env, jclass class) {
+ int i;
+ i = -1;
+ memcpy(&i, &i, i);
+ memset(&i, ' ', 1024);
+}
+
+#ifdef __cplusplus
+}
+#endif
+#endif
Added: tomee/deps/branches/commons-daemon/src/samples/Native.sh
URL: http://svn.apache.org/viewvc/tomee/deps/branches/commons-daemon/src/samples/Native.sh?rev=1860225&view=auto
==============================================================================
--- tomee/deps/branches/commons-daemon/src/samples/Native.sh (added)
+++ tomee/deps/branches/commons-daemon/src/samples/Native.sh Tue May 28 09:46:53 2019
@@ -0,0 +1,34 @@
+#!/bin/sh
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# 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.
+#
+# for linux ;-) and Mac OS X
+case `uname -s` in
+ Darwin)
+ JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
+ INCLUDE=Headers
+ OS=
+ FLAGS=-dynamiclib
+ ;;
+ Linux)
+ OS=linux
+ INCLUDE=include
+ FLAGS=-shared
+ ;;
+esac
+
+gcc -c -I${JAVA_HOME}/${INCLUDE} -I${JAVA_HOME}/${INCLUDE}/${OS} Native.c
+gcc ${FLAGS} -o Native.so Native.o
Propchange: tomee/deps/branches/commons-daemon/src/samples/Native.sh
------------------------------------------------------------------------------
svn:executable = *
Added: tomee/deps/branches/commons-daemon/src/samples/ProcrunService.java
URL: http://svn.apache.org/viewvc/tomee/deps/branches/commons-daemon/src/samples/ProcrunService.java?rev=1860225&view=auto
==============================================================================
--- tomee/deps/branches/commons-daemon/src/samples/ProcrunService.java (added)
+++ tomee/deps/branches/commons-daemon/src/samples/ProcrunService.java Tue May 28 09:46:53 2019
@@ -0,0 +1,244 @@
+import java.io.File;
+import java.io.IOException;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+//import java.util.Enumeration;
+import java.util.Iterator;
+//import java.util.List;
+//import java.util.Map;
+import java.util.Properties;
+import java.util.TreeSet;
+//import java.net.InterfaceAddress;
+//import java.net.NetworkInterface;
+//import java.net.SocketException;
+
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ *
+ */
+
+/**
+ * Sample service implementation for use with Windows Procrun.
+ * <p>
+ * Use the main() method for running as a Java (external) service.
+ * Use the start() and stop() methods for running as a jvm (in-process) service
+ */
+public class ProcrunService implements Runnable {
+
+ private static final int DEFAULT_PAUSE = 60; // Wait 1 minute
+ private static final long MS_PER_SEC = 1000L; // Milliseconds in a second
+
+ private static volatile Thread thrd; // start and stop are called from different threads
+
+ private final long pause; // How long to pause in service loop
+
+ private final File stopFile;
+
+ /**
+ *
+ * @param wait seconds to wait in loop
+ * @param file optional file - if non-null, run loop will stop when it disappears
+ */
+ private ProcrunService(long wait, File file) {
+ pause=wait;
+ stopFile = file;
+ }
+
+ private static File tmpFile(String filename) {
+ return new File(System.getProperty("java.io.tmpdir"),
+ filename != null ? filename : "ProcrunService.tmp");
+ }
+
+ private static void usage(){
+ System.err.println("Must supply the argument 'start' or 'stop'");
+ }
+
+ /**
+ * Helper method for process args with defaults.
+ *
+ * @param args array of string arguments, may be empty
+ * @param argnum which argument to extract
+ * @return the argument or null
+ */
+ private static String getArg(String[] args, int argnum){
+ if (args.length > argnum) {
+ return args[argnum];
+ } else {
+ return null;
+ }
+ }
+
+ private static void logSystemEnvironment()
+ {
+ ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ if (cl == null)
+ log("Missing currentThread context ClassLoader");
+ else
+ log("Using context ClassLoader : " + cl.toString());
+ log("Program environment: ");
+
+// Java 1.5+ code
+// Map em = System.getenv();
+// TreeSet es = new TreeSet(em.keySet());
+// for (Iterator i = es.iterator(); i.hasNext();) {
+// String n = (String)i.next();
+// log(n + " -> " + em.get(n));
+// }
+
+ log("System properties: ");
+ Properties ps = System.getProperties();
+ TreeSet<Object> ts = new TreeSet<Object>(ps.keySet());
+ for (Iterator<Object> i = ts.iterator(); i.hasNext();) {
+ String n = (String)i.next();
+ log(n + " -> " + ps.get(n));
+ }
+
+// Java 1.6+ code
+// log("Network interfaces: ");
+// log("LVPMU (L)oopback (V)irtual (P)ointToPoint (M)multicastSupport (U)p");
+// try {
+// for (Enumeration e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
+// NetworkInterface n = (NetworkInterface)e.nextElement();
+// char [] flags = { '-', '-', '-', '-', '-'};
+// if (n.isLoopback())
+// flags[0] = 'x';
+// if (n.isVirtual())
+// flags[1] = 'x';
+// if (n.isPointToPoint())
+// flags[2] = 'x';
+// if (n.supportsMulticast())
+// flags[3] = 'x';
+// if (n.isUp())
+// flags[4] = 'x';
+// String neti = new String(flags) + " " + n.getName() + "\t";
+// for (Enumeration i = n.getSubInterfaces(); i.hasMoreElements();) {
+// NetworkInterface s = (NetworkInterface)i.nextElement();
+// neti += " [" + s.getName() + "]";
+// }
+// log(neti + " -> " + n.getDisplayName());
+// List i = n.getInterfaceAddresses();
+// if (!i.isEmpty()) {
+// for (int x = 0; x < i.size(); x++) {
+// InterfaceAddress a = (InterfaceAddress)i.get(x);
+// log(" " + a.toString());
+// }
+// }
+// }
+// } catch (SocketException e) {
+// // Ignore
+// }
+ }
+
+ /**
+ * Common entry point for start and stop service functions.
+ * To allow for use with Java mode, a temporary file is created
+ * by the start service, and a deleted by the stop service.
+ *
+ * @param args [start [pause time] | stop]
+ * @throws IOException if there are problems creating or deleting the temporary file
+ */
+ public static void main(String[] args) throws IOException {
+ final int argc = args.length;
+ log("ProcrunService called with "+argc+" arguments from thread: "+Thread.currentThread());
+ for(int i=0; i < argc; i++) {
+ System.out.println("["+i+"] "+args[i]);
+ }
+ String mode=getArg(args, 0);
+ if ("start".equals(mode)){
+ File f = tmpFile(getArg(args, 2));
+ log("Creating file: "+f.getPath());
+ f.createNewFile();
+ startThread(getArg(args, 1), f);
+ } else if ("stop".equals(mode)) {
+ final File tmpFile = tmpFile(getArg(args, 1));
+ log("Deleting file: "+tmpFile.getPath());
+ tmpFile.delete();
+ } else {
+ usage();
+ }
+ }
+
+ /**
+ * Start the jvm version of the service, and waits for it to complete.
+ *
+ * @param args optional, arg[0] = timeout (seconds)
+ */
+ public static void start(String [] args) {
+ startThread(getArg(args, 0), null);
+ while(thrd.isAlive()){
+ try {
+ thrd.join();
+ } catch (InterruptedException ie){
+ // Ignored
+ }
+ }
+ }
+
+ private static void startThread(String waitParam, File file) {
+ long wait = DEFAULT_PAUSE;
+ if (waitParam != null) {
+ wait = Integer.valueOf(waitParam).intValue();
+ }
+ log("Starting the thread, wait(seconds): "+wait);
+ thrd = new Thread(new ProcrunService(wait*MS_PER_SEC,file));
+ thrd.start();
+ }
+
+ /**
+ * Stop the JVM version of the service.
+ *
+ * @param args ignored
+ */
+ public static void stop(String [] args){
+ if (thrd != null) {
+ log("Interrupting the thread");
+ thrd.interrupt();
+ } else {
+ log("No thread to interrupt");
+ }
+ }
+
+ /**
+ * This method performs the work of the service.
+ * In this case, it just logs a message every so often.
+ */
+ @Override
+ public void run() {
+ log("Started thread in "+System.getProperty("user.dir"));
+ logSystemEnvironment();
+ while(stopFile == null || stopFile.exists()){
+ try {
+ log("pausing...");
+ Thread.sleep(pause);
+ } catch (InterruptedException e) {
+ log("Exitting");
+ break;
+ }
+ }
+ }
+
+ private static void log(String msg){
+ DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
+ System.out.println(df.format(new Date())+msg);
+ }
+
+ @Override
+ protected void finalize(){
+ log("Finalize called from thread "+Thread.currentThread());
+ }
+}
Added: tomee/deps/branches/commons-daemon/src/samples/ProcrunServiceInstall.cmd
URL: http://svn.apache.org/viewvc/tomee/deps/branches/commons-daemon/src/samples/ProcrunServiceInstall.cmd?rev=1860225&view=auto
==============================================================================
--- tomee/deps/branches/commons-daemon/src/samples/ProcrunServiceInstall.cmd (added)
+++ tomee/deps/branches/commons-daemon/src/samples/ProcrunServiceInstall.cmd Tue May 28 09:46:53 2019
@@ -0,0 +1,65 @@
+@echo off
+rem
+rem Licensed to the Apache Software Foundation (ASF) under one or more
+rem contributor license agreements. See the NOTICE file distributed with
+rem this work for additional information regarding copyright ownership.
+rem The ASF licenses this file to You under the Apache License, Version 2.0
+rem (the "License"); you may not use this file except in compliance with
+rem the License. You may obtain a copy of the License at
+rem
+rem http://www.apache.org/licenses/LICENSE-2.0
+rem
+rem Unless required by applicable law or agreed to in writing, software
+rem distributed under the License is distributed on an "AS IS" BASIS,
+rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+rem See the License for the specific language governing permissions and
+rem limitations under the License.
+
+rem Batch script for defining the ProcrunService (JVM and Java versions)
+
+rem Copy this file and ProcrunService.jar into the same directory as prunsrv (or adjust the paths below)
+
+setlocal
+
+rem The service names (make sure they does not clash with an existing service)
+set SERVICE_JVM=ProcrunServiceJvm
+set SERVICE_JAVA=ProcrunServiceJava
+
+rem my location
+set MYPATH=%~dp0
+
+rem location of Prunsrv
+set PATH_PRUNSRV=%MYPATH%
+set PR_LOGPATH=%PATH_PRUNSRV%
+rem location of jarfile
+set PATH_JAR=%MYPATH%
+
+rem Allow prunsrv to be overridden
+if "%PRUNSRV%" == "" set PRUNSRV=%PATH_PRUNSRV%prunsrv
+
+rem Install the 2 services
+
+echo Installing %SERVICE_JVM%
+%PRUNSRV% //DS//%SERVICE_JVM%
+%PRUNSRV% //IS//%SERVICE_JVM%
+
+echo Setting the parameters for %SERVICE_JVM%
+%PRUNSRV% //US//%SERVICE_JVM% --Jvm=auto --StdOutput auto --StdError auto ^
+--Classpath=%PATH_JAR%ProcrunService.jar ^
+--StartMode=jvm --StartClass=ProcrunService --StartMethod=start ^
+ --StopMode=jvm --StopClass=ProcrunService --StopMethod=stop
+
+echo Installation of %SERVICE_JVM% is complete
+
+echo Installing %SERVICE_JAVA%
+%PRUNSRV% //DS//%SERVICE_JAVA%
+%PRUNSRV% //IS//%SERVICE_JAVA%
+
+echo Setting the parameters for %SERVICE_JAVA%
+%PRUNSRV% //US//%SERVICE_JAVA% --Jvm=auto --StdOutput auto --StdError auto ^
+--Classpath=%PATH_JAR%ProcrunService.jar ^
+--StartMode=java --StartClass=ProcrunService --StartParams=start ^
+ --StopMode=java --StopClass=ProcrunService --StopParams=stop
+
+echo Installation of %SERVICE_JAVA% is complete
+echo Finished
Added: tomee/deps/branches/commons-daemon/src/samples/ProcrunServiceRemove.cmd
URL: http://svn.apache.org/viewvc/tomee/deps/branches/commons-daemon/src/samples/ProcrunServiceRemove.cmd?rev=1860225&view=auto
==============================================================================
--- tomee/deps/branches/commons-daemon/src/samples/ProcrunServiceRemove.cmd (added)
+++ tomee/deps/branches/commons-daemon/src/samples/ProcrunServiceRemove.cmd Tue May 28 09:46:53 2019
@@ -0,0 +1,42 @@
+@echo off
+rem
+rem Licensed to the Apache Software Foundation (ASF) under one or more
+rem contributor license agreements. See the NOTICE file distributed with
+rem this work for additional information regarding copyright ownership.
+rem The ASF licenses this file to You under the Apache License, Version 2.0
+rem (the "License"); you may not use this file except in compliance with
+rem the License. You may obtain a copy of the License at
+rem
+rem http://www.apache.org/licenses/LICENSE-2.0
+rem
+rem Unless required by applicable law or agreed to in writing, software
+rem distributed under the License is distributed on an "AS IS" BASIS,
+rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+rem See the License for the specific language governing permissions and
+rem limitations under the License.
+
+rem Batch script for removing the ProcrunService (JVM and Java versions)
+
+setlocal
+
+rem The service names (make sure they does not clash with an existing service)
+set SERVICE_JVM=ProcrunServiceJvm
+set SERVICE_JAVA=ProcrunServiceJava
+
+rem my location
+set MYPATH=%~dp0
+
+rem location of Prunsrv
+set PATH_PRUNSRV=%MYPATH%
+set PR_LOGPATH=%PATH_PRUNSRV%
+rem Allow prunsrv to be overridden
+if "%PRUNSRV%" == "" set PRUNSRV=%PATH_PRUNSRV%prunsrv
+
+echo Removing %SERVICE_JVM%
+%PRUNSRV% //DS//%SERVICE_JVM%
+
+echo Removing %SERVICE_JAVA%
+%PRUNSRV% //DS//%SERVICE_JAVA%
+%PRUNSRV% //IS//%SERVICE_JAVA%
+
+echo Finished
Added: tomee/deps/branches/commons-daemon/src/samples/README.txt
URL: http://svn.apache.org/viewvc/tomee/deps/branches/commons-daemon/src/samples/README.txt?rev=1860225&view=auto
==============================================================================
--- tomee/deps/branches/commons-daemon/src/samples/README.txt (added)
+++ tomee/deps/branches/commons-daemon/src/samples/README.txt Tue May 28 09:46:53 2019
@@ -0,0 +1,49 @@
+The directory contains examples of Java daemons.
+The examples are compiled using Ant (just type ant). Each example creates a
+jar file in ../../dist
+
+SimpleDaemon
+------------
+
+SimpleDaemon demonstrates the feature of the daemon offered by
+Apache Commons Daemon.
+To run it adapt the SimpleDaemon.sh file and connect to it using:
+telnet localhost 1200
+Additional information in ../native/unix/INSTALL.txt
+
+ServiceDaemon
+-------------
+
+ServiceDaemon allows to start programs using the Commons Daemon.
+
+That could be useful when using Cygwin under Win9x because Cygwin only offers
+services support under Win NT/2000/XP.
+(See in ../native/nt/README how to install jsvc as a service in Win32).
+
+It uses Apache Commons Collections:
+http://commons.apache.org/collections/
+To use it you need at least commons-collections-1.0
+Check in build.xml that the property commons-collections.jar correspond to thei
+location of your commons-collections.jar file.
+
+You have to create a file named startfile that uses a property format:
+name = string to start the program
+
+For example:
+sshd=/usr/sbin/sshd -D
+router1=/home/Standard/router/router pop3 pop3.example.net
+router2=/home/Standard/router/smtp smtp.example.net
+socks5=/usr/local/bin/socks5 -f
+
+To run it adapt the ServiceDaemon.sh file.
+
+AloneService
+------------
+
+AloneService is like ServiceDaemon except it does not use the Daemon interface.
+
+ProcrunService
+--------------
+This is a simple Windows Service application.
+It can be run either in Jvm or Java modes.
+See ProcrunServiceInstall.cmd for a sample installation script.
\ No newline at end of file
Added: tomee/deps/branches/commons-daemon/src/samples/ServiceDaemon.java
URL: http://svn.apache.org/viewvc/tomee/deps/branches/commons-daemon/src/samples/ServiceDaemon.java?rev=1860225&view=auto
==============================================================================
--- tomee/deps/branches/commons-daemon/src/samples/ServiceDaemon.java (added)
+++ tomee/deps/branches/commons-daemon/src/samples/ServiceDaemon.java Tue May 28 09:46:53 2019
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.util.Enumeration;
+import java.util.Properties;
+
+import org.apache.commons.daemon.Daemon;
+import org.apache.commons.daemon.DaemonContext;
+
+public class ServiceDaemon implements Daemon {
+
+ private Properties prop = null;
+ private Process proc[] = null;
+ private ServiceDaemonReadThread readout[] = null;
+ private ServiceDaemonReadThread readerr[] = null;
+
+ public ServiceDaemon() {
+ super();
+ System.err.println("ServiceDaemon: instance "+this.hashCode()+
+ " created");
+ }
+
+ @Override
+ protected void finalize() {
+ System.err.println("ServiceDaemon: instance "+this.hashCode()+
+ " garbage collected");
+ }
+
+ /**
+ * init and destroy were added in jakarta-tomcat-daemon.
+ */
+ @Override
+ public void init(DaemonContext context)
+ throws Exception {
+ /* Set the err */
+ System.setErr(new PrintStream(new FileOutputStream(new File("ServiceDaemon.err")),true));
+ System.err.println("ServiceDaemon: instance "+this.hashCode()+
+ " init");
+
+ /* read the properties file */
+ prop = new Properties();
+ try {
+ prop.load(new FileInputStream("startfile"));
+ }
+ catch (Exception e) {
+ // Cannot find startfile.properties.
+ // XXX: Should we print something?
+ }
+ /* create an array to store the processes */
+ int processCount = prop.size();
+ System.err.println("ServiceDaemon: init for " + processCount + " processes");
+ proc = new Process[processCount];
+ readout = new ServiceDaemonReadThread[processCount];
+ readerr = new ServiceDaemonReadThread[processCount];
+ for (int i = 0 ; i < processCount; i++) {
+ proc[i] = null;
+ readout[i] = null;
+ readerr[i] = null;
+ }
+
+ System.err.println("ServiceDaemon: init done ");
+ }
+
+ @Override
+ public void start() {
+ /* Dump a message */
+ System.err.println("ServiceDaemon: starting");
+
+ /* Start */
+ int i=0;
+ for (Enumeration<Object> e = prop.keys(); e.hasMoreElements() ;) {
+ String name = (String) e.nextElement();
+ System.err.println("ServiceDaemon: starting: " + name + " : " + prop.getProperty(name));
+ try {
+ proc[i] = Runtime.getRuntime().exec(prop.getProperty(name));
+ } catch(Exception ex) {
+ System.err.println("Exception: " + ex);
+ }
+ /* Start threads to read from Error and Out streams */
+ readerr[i] =
+ new ServiceDaemonReadThread(proc[i].getErrorStream());
+ readout[i] =
+ new ServiceDaemonReadThread(proc[i].getInputStream());
+ readerr[i].start();
+ readout[i].start();
+ i++;
+ }
+ }
+
+ @Override
+ public void stop()
+ throws IOException, InterruptedException {
+ /* Dump a message */
+ System.err.println("ServiceDaemon: stopping");
+
+ for (int i=0;i<proc.length;i++) {
+ if ( proc[i]==null)
+ continue;
+ proc[i].destroy();
+ try {
+ proc[i].waitFor();
+ } catch(InterruptedException ex) {
+ System.err.println("ServiceDaemon: exception while stopping:" +
+ ex);
+ }
+ }
+
+ System.err.println("ServiceDaemon: stopped");
+ }
+
+ @Override
+ public void destroy() {
+ System.err.println("ServiceDaemon: instance "+this.hashCode()+
+ " destroy");
+ }
+
+}
Added: tomee/deps/branches/commons-daemon/src/samples/ServiceDaemon.sh
URL: http://svn.apache.org/viewvc/tomee/deps/branches/commons-daemon/src/samples/ServiceDaemon.sh?rev=1860225&view=auto
==============================================================================
--- tomee/deps/branches/commons-daemon/src/samples/ServiceDaemon.sh (added)
+++ tomee/deps/branches/commons-daemon/src/samples/ServiceDaemon.sh Tue May 28 09:46:53 2019
@@ -0,0 +1,39 @@
+#!/bin/sh
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# 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.
+#
+# Small shell script to show how to start the sample services.
+#
+# Adapt the following lines to your configuration
+JAVA_HOME=/cygdrive/c/jdk1.3.1_02
+HOME=c:\\cygwin\\home\\Standard
+DAEMON_HOME=$HOME\\jakarta-commons-sandbox\\daemon
+DAEMON_HOME_SH=/home/Standard/jakarta-commons-sandbox/daemon
+TOMCAT_USER=jakarta
+CLASSPATH=\
+$DAEMON_HOME\\dist\\commons-daemon.jar\;\
+$HOME\\commons-collections-1.0\\commons-collections.jar\;\
+$DAEMON_HOME\\dist\\Service.jar
+
+$DAEMON_HOME_SH/dist/jsvc \
+ -home $JAVA_HOME \
+ -cp $CLASSPATH \
+ ServiceDaemon
+#
+# To get a verbose JVM
+#-verbose \
+# To get a debug of jsvc.
+#-debug \
Propchange: tomee/deps/branches/commons-daemon/src/samples/ServiceDaemon.sh
------------------------------------------------------------------------------
svn:executable = *
Added: tomee/deps/branches/commons-daemon/src/samples/ServiceDaemonReadThread.java
URL: http://svn.apache.org/viewvc/tomee/deps/branches/commons-daemon/src/samples/ServiceDaemonReadThread.java?rev=1860225&view=auto
==============================================================================
--- tomee/deps/branches/commons-daemon/src/samples/ServiceDaemonReadThread.java (added)
+++ tomee/deps/branches/commons-daemon/src/samples/ServiceDaemonReadThread.java Tue May 28 09:46:53 2019
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+
+import java.io.InputStream;
+import java.io.IOException;
+import java.lang.Thread;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+
+public class ServiceDaemonReadThread extends Thread {
+ private BufferedReader in;
+ ServiceDaemonReadThread(InputStream in) {
+ this.in = new BufferedReader(new InputStreamReader(in));
+ }
+ @Override
+ public void run() {
+ String buff;
+ for (;;) {
+ try {
+ buff = in.readLine();
+ if (buff == null) break;
+ System.err.print(in.readLine());
+ } catch (IOException ex) {
+ break; // Exit thread.
+ }
+ }
+ }
+}
Added: tomee/deps/branches/commons-daemon/src/samples/SimpleApplication.java
URL: http://svn.apache.org/viewvc/tomee/deps/branches/commons-daemon/src/samples/SimpleApplication.java?rev=1860225&view=auto
==============================================================================
--- tomee/deps/branches/commons-daemon/src/samples/SimpleApplication.java (added)
+++ tomee/deps/branches/commons-daemon/src/samples/SimpleApplication.java Tue May 28 09:46:53 2019
@@ -0,0 +1,366 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+//import java.io.UnsupportedEncodingException;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Enumeration;
+import java.util.Vector;
+
+public class SimpleApplication implements Runnable {
+
+ private ServerSocket server=null;
+ private Thread thread=null;
+ private volatile boolean stopping=false;
+ private String directory=null;
+ private final Vector<Handler> handlers;
+
+ public static native void toto();
+
+ public SimpleApplication()
+ {
+ super();
+ System.err.println("SimpleApplication: instance "+this.hashCode()+
+ " created");
+ this.handlers = new Vector<Handler>();
+ }
+
+ @Override
+ protected void finalize() {
+ System.err.println("SimpleApplication: instance "+this.hashCode()+
+ " garbage collected");
+ }
+
+ /**
+ * Main method
+ *
+ * @param args Optional port and directory configuration
+ *
+ * @throws Exception If the daemon cannot be set up
+ */
+ public static void main(String[] args) throws Exception {
+ SimpleApplication app = new SimpleApplication();
+ System.err.println("SimpleApplication: instance " + app.hashCode()+
+ " init " + args.length);
+ int port=1200;
+ for (int i = 0; i < args.length; i++) {
+ System.err.println("SimpleApplication: arg " + i +
+ " = " + args[i]);
+
+
+ }
+ if (args.length > 0 && args[0].length() > 0)
+ port=Integer.parseInt(args[0]);
+ if (args.length > 1)
+ app.directory = args[1];
+ else
+ app.directory="/tmp";
+
+ /* Dump a message */
+ System.err.println("SimpleApplication: loading on port "+port);
+ Runtime.getRuntime().addShutdownHook(new ShutdownHook(app));
+ /* Set up this simple daemon */
+ app.server = new ServerSocket(port);
+ app.thread = new Thread(app);
+ app.start();
+ }
+
+ public void start()
+ {
+ /* Dump a message */
+ System.err.println("SimpleApplication: starting");
+
+ /* Start */
+ this.thread.start();
+ }
+
+ public void stop()
+ throws IOException, InterruptedException
+ {
+ /* Dump a message */
+ System.err.println("SimpleApplication: stopping");
+
+ /* Close the ServerSocket. This will make our thread to terminate */
+ this.stopping=true;
+ this.server.close();
+
+ /* Wait for the main thread to exit and dump a message */
+ this.thread.join(5000);
+ System.err.println("SimpleApplication: stopped");
+ }
+
+ public void destroy()
+ {
+ System.err.println("SimpleApplication: instance "+this.hashCode()+
+ " destroy");
+ }
+
+ @Override
+ public void run()
+ {
+ int number=0;
+
+ System.err.println("SimpleApplication: started acceptor loop");
+ try {
+ while(!this.stopping) {
+ Socket socket=this.server.accept();
+ Handler handler=new Handler(socket,this);
+ handler.setConnectionNumber(number++);
+ handler.setDirectoryName(this.directory);
+ new Thread(handler).start();
+ }
+ } catch (IOException e) {
+ /* Don't dump any error message if we are stopping. A IOException
+ is generated when the ServerSocket is closed in stop() */
+ if (!this.stopping) e.printStackTrace(System.err);
+ }
+
+ /* Terminate all handlers that at this point are still open */
+ Enumeration<Handler> openhandlers = this.handlers.elements();
+ while (openhandlers.hasMoreElements()) {
+ Handler handler = openhandlers.nextElement();
+ System.err.println("SimpleApplication: dropping connection "+
+ handler.getConnectionNumber());
+ handler.close();
+ }
+
+ System.err.println("SimpleApplication: exiting acceptor loop");
+ }
+
+ protected void addHandler(Handler handler) {
+ synchronized (handler) {
+ this.handlers.add(handler);
+ }
+ }
+
+ protected void removeHandler(Handler handler) {
+ synchronized (handler) {
+ this.handlers.remove(handler);
+ }
+ }
+
+ public static class ShutdownHook extends Thread
+ {
+ private final SimpleApplication instance;
+
+ public ShutdownHook(SimpleApplication instance)
+ {
+ this.instance = instance;
+ }
+ @Override
+ public void run()
+ {
+ System.out.println("Shutting down");
+ try {
+ instance.stop();
+ }
+ catch (Exception e) {
+ e.printStackTrace(System.err);
+ }
+ }
+ }
+
+ public static class Handler implements Runnable {
+
+ private final SimpleApplication parent;
+ private String directory=null; // Only set before thread is started
+ private final Socket socket;
+ private int number=0; // Only set before thread is started
+
+ public Handler(Socket s, SimpleApplication p) {
+ super();
+ this.socket=s;
+ this.parent=p;
+ }
+
+ @Override
+ public void run() {
+ this.parent.addHandler(this);
+ System.err.println("SimpleApplication: connection "+this.number+
+ " opened from "+this.socket.getInetAddress());
+ try {
+ InputStream in=this.socket.getInputStream();
+ OutputStream out=this.socket.getOutputStream();
+ handle(in,out);
+ this.socket.close();
+ } catch (IOException e) {
+ e.printStackTrace(System.err);
+ }
+ System.err.println("SimpleApplication: connection "+this.number+
+ " closed");
+ this.parent.removeHandler(this);
+ }
+
+ public void close() {
+ try {
+ this.socket.close();
+ } catch (IOException e) {
+ e.printStackTrace(System.err);
+ }
+ }
+
+ public void setConnectionNumber(int number) {
+ this.number=number;
+ }
+
+ public int getConnectionNumber() {
+ return(this.number);
+ }
+
+ public void setDirectoryName(String directory) {
+ this.directory=directory;
+ }
+
+ public String getDirectoryName() {
+ return(this.directory);
+ }
+
+ public void createFile(String name)
+ throws IOException {
+ OutputStream file=new FileOutputStream(name,true);
+ PrintStream out=new PrintStream(file);
+ SimpleDateFormat fmt=new SimpleDateFormat();
+
+ out.println(fmt.format(new Date()));
+ out.close();
+ file.close();
+ }
+
+ public void createDir(String name)
+ throws IOException {
+ File file = new File(name);
+ boolean ok = file.mkdirs();
+ if(! ok)
+ throw new IOException("mkdirs for "+name+" failed");
+ createFile(name);
+ }
+
+ public void handle(InputStream in, OutputStream os) {
+ PrintStream out=null;
+// try {
+// out=new PrintStream(os, true, "US-ASCII"); // Java 1.4+
+// } catch (UnsupportedEncodingException ex) {
+ out=new PrintStream(os, true);
+// }
+
+ while(true) {
+ try {
+ /* If we don't have data in the System InputStream, we want
+ to ask to the user for an option. */
+ if (in.available()==0) {
+ out.println();
+ out.println("Please select one of the following:");
+ out.println(" 1) Shutdown");
+ out.println(" 2) Create a file");
+ out.println(" 3) Disconnect");
+ out.println(" 4) Cause a core of the JVM");
+ out.println(" 5) Create a directory");
+ out.print("Your choice: ");
+ }
+
+ /* Read an option from the client */
+ int x=in.read();
+
+ switch (x) {
+ /* If the socket was closed, we simply return */
+ case -1:
+ return;
+
+ /* Attempt to shutdown */
+ case '1':
+ out.println("Attempting a shutdown...");
+ try {
+ System.exit(0);
+ } catch (IllegalStateException e) {
+ out.println();
+ out.println("Can't shutdown now");
+ e.printStackTrace(out);
+ }
+ break;
+
+ /* Create a file */
+ case '2':
+ String name=this.getDirectoryName()+
+ "/SimpleApplication."+
+ this.getConnectionNumber()+
+ ".tmp";
+ try {
+ this.createFile(name);
+ out.println("File '"+name+"' created");
+ } catch (IOException e) {
+ e.printStackTrace(out);
+ }
+ break;
+
+ /* Disconnect */
+ case '3':
+ out.println("Disconnecting...");
+ return;
+
+ /* Crash JVM in a native call: It need an so file ;-) */
+ case '4':
+ System.load(System.getProperty("native.library", "./Native.so"));
+ toto();
+ break;
+
+ /* Create a directory (PR 30177 with 1.4.x and 1.5.0 */
+ case '5':
+ String name1=this.getDirectoryName()+
+ "/a/b/c/d/e"+
+ "/SimpleApplication."+
+ this.getConnectionNumber()+
+ ".tmp";
+ try {
+ this.createDir(name1);
+ out.println("File '"+name1+"' created");
+ } catch (IOException e) {
+ e.printStackTrace(out);
+ }
+ break;
+
+
+ /* Discard any carriage return / newline characters */
+ case '\r':
+ case '\n':
+ break;
+
+ /* We got something that we weren't supposed to get */
+ default:
+ out.println("Unknown option '"+(char)x+"'");
+ break;
+
+ }
+
+ /* If we get an IOException we return (disconnect) */
+ } catch (IOException e) {
+ System.err.println("SimpleApplication: IOException in "+
+ "connection "+
+ this.getConnectionNumber());
+ return;
+ }
+ }
+ }
+ }
+}
Added: tomee/deps/branches/commons-daemon/src/samples/SimpleApplication.sh
URL: http://svn.apache.org/viewvc/tomee/deps/branches/commons-daemon/src/samples/SimpleApplication.sh?rev=1860225&view=auto
==============================================================================
--- tomee/deps/branches/commons-daemon/src/samples/SimpleApplication.sh (added)
+++ tomee/deps/branches/commons-daemon/src/samples/SimpleApplication.sh Tue May 28 09:46:53 2019
@@ -0,0 +1,57 @@
+#!/bin/sh
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# 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.
+#
+# Small shell script to show how to start the sample services.
+#
+# Adapt the following lines to your configuration
+JAVA_HOME=/opt/java6
+PROGRAM=SimpleApplication
+CLASSPATH=`pwd`/$PROGRAM.jar:`pwd`/commons-daemon-1.1.1.jar
+
+case "$1" in
+ start )
+ shift
+ ./jsvc \
+ -home $JAVA_HOME \
+ -cp $CLASSPATH \
+ -nodetach \
+ -errfile "&2" \
+ -pidfile `pwd`/$PROGRAM.pid \
+ @$PROGRAM \
+ -start-method main \
+ $*
+ exit $?
+ ;;
+ stop )
+ shift
+ ./jsvc \
+ -home $JAVA_HOME \
+ -cp $CLASSPATH \
+ -stop \
+ -nodetach \
+ -errfile "&2" \
+ -pidfile `pwd`/$PROGRAM.pid \
+ @$PROGRAM \
+ -start-method main \
+ $*
+ exit $?
+ ;;
+ * )
+ echo 'Usage SimpleApplication.sh start | stop'
+ exit 1
+ ;;
+esac
Propchange: tomee/deps/branches/commons-daemon/src/samples/SimpleApplication.sh
------------------------------------------------------------------------------
svn:executable = *
Added: tomee/deps/branches/commons-daemon/src/samples/SimpleDaemon.java
URL: http://svn.apache.org/viewvc/tomee/deps/branches/commons-daemon/src/samples/SimpleDaemon.java?rev=1860225&view=auto
==============================================================================
--- tomee/deps/branches/commons-daemon/src/samples/SimpleDaemon.java (added)
+++ tomee/deps/branches/commons-daemon/src/samples/SimpleDaemon.java Tue May 28 09:46:53 2019
@@ -0,0 +1,361 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+//import java.io.UnsupportedEncodingException;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Enumeration;
+import java.util.Vector;
+
+import org.apache.commons.daemon.Daemon;
+import org.apache.commons.daemon.DaemonContext;
+import org.apache.commons.daemon.DaemonController;
+import org.apache.commons.daemon.DaemonInitException;
+
+public class SimpleDaemon implements Daemon, Runnable {
+
+ private ServerSocket server=null;
+ private Thread thread=null;
+ private DaemonController controller=null;
+ private volatile boolean stopping=false;
+ private String directory=null;
+ private final Vector<Handler> handlers;
+
+ public static native void toto();
+
+ public SimpleDaemon() {
+ super();
+ System.err.println("SimpleDaemon: instance "+this.hashCode()+
+ " created");
+ this.handlers = new Vector<Handler>();
+ }
+
+ @Override
+ protected void finalize() {
+ System.err.println("SimpleDaemon: instance "+this.hashCode()+
+ " garbage collected");
+ }
+
+ /**
+ * init and destroy were added in jakarta-tomcat-daemon.
+ */
+ @Override
+ public void init(DaemonContext context)
+ throws Exception {
+ System.err.println("SimpleDaemon: instance "+this.hashCode()+
+ " init");
+
+ int port=1200;
+
+ String[] a = context.getArguments();
+ try {
+ if ( a.length > 0)
+ port=Integer.parseInt(a[0]);
+ }
+ catch (NumberFormatException ex) {
+ throw new DaemonInitException("parsing port", ex);
+ }
+ if (a.length>1) this.directory=a[1];
+ else this.directory="/tmp";
+
+ /* Dump a message */
+ System.err.println("SimpleDaemon: loading on port "+port);
+
+ /* Set up this simple daemon */
+ this.controller=context.getController();
+ this.server=new ServerSocket(port);
+ this.thread=new Thread(this);
+ }
+
+ @Override
+ public void start() {
+ /* Dump a message */
+ System.err.println("SimpleDaemon: starting");
+
+ /* Start */
+ this.thread.start();
+ }
+
+ @Override
+ public void stop()
+ throws IOException, InterruptedException {
+ /* Dump a message */
+ System.err.println("SimpleDaemon: stopping");
+
+ /* Close the ServerSocket. This will make our thread to terminate */
+ this.stopping=true;
+ this.server.close();
+
+ /* Wait for the main thread to exit and dump a message */
+ this.thread.join(5000);
+ System.err.println("SimpleDaemon: stopped");
+ }
+
+ @Override
+ public void destroy() {
+ System.err.println("SimpleDaemon: instance "+this.hashCode()+
+ " destroy");
+ }
+
+ @Override
+ public void run() {
+ int number=0;
+
+ System.err.println("SimpleDaemon: started acceptor loop");
+ try {
+ while(!this.stopping) {
+ Socket socket=this.server.accept();
+ Handler handler=new Handler(socket,this,this.controller);
+ handler.setConnectionNumber(number++);
+ handler.setDirectoryName(this.directory);
+ new Thread(handler).start();
+ }
+ } catch (IOException e) {
+ /* Don't dump any error message if we are stopping. A IOException
+ is generated when the ServerSocket is closed in stop() */
+ if (!this.stopping) e.printStackTrace(System.err);
+ }
+
+ /* Terminate all handlers that at this point are still open */
+ Enumeration<Handler> openhandlers = this.handlers.elements();
+ while (openhandlers.hasMoreElements()) {
+ Handler handler = openhandlers.nextElement();
+ System.err.println("SimpleDaemon: dropping connection "+
+ handler.getConnectionNumber());
+ handler.close();
+ }
+
+ System.err.println("SimpleDaemon: exiting acceptor loop");
+ }
+
+ protected void addHandler(Handler handler) {
+ synchronized (handler) {
+ this.handlers.add(handler);
+ }
+ }
+
+ protected void removeHandler(Handler handler) {
+ synchronized (handler) {
+ this.handlers.remove(handler);
+ }
+ }
+
+ public static class Handler implements Runnable {
+
+ private final DaemonController controller;
+ private final SimpleDaemon parent;
+ private String directory=null; // Only set before thread is started
+ private final Socket socket;
+ private int number=0; // Only set before thread is started
+
+ public Handler(Socket s, SimpleDaemon p, DaemonController c) {
+ super();
+ this.socket=s;
+ this.parent=p;
+ this.controller=c;
+ }
+
+ @Override
+ public void run() {
+ this.parent.addHandler(this);
+ System.err.println("SimpleDaemon: connection "+this.number+
+ " opened from "+this.socket.getInetAddress());
+ try {
+ InputStream in=this.socket.getInputStream();
+ OutputStream out=this.socket.getOutputStream();
+ handle(in,out);
+ this.socket.close();
+ } catch (IOException e) {
+ e.printStackTrace(System.err);
+ }
+ System.err.println("SimpleDaemon: connection "+this.number+
+ " closed");
+ this.parent.removeHandler(this);
+ }
+
+ public void close() {
+ try {
+ this.socket.close();
+ } catch (IOException e) {
+ e.printStackTrace(System.err);
+ }
+ }
+
+ public void setConnectionNumber(int number) {
+ this.number=number;
+ }
+
+ public int getConnectionNumber() {
+ return(this.number);
+ }
+
+ public void setDirectoryName(String directory) {
+ this.directory=directory;
+ }
+
+ public String getDirectoryName() {
+ return(this.directory);
+ }
+
+ public void createFile(String name)
+ throws IOException {
+ OutputStream file=new FileOutputStream(name,true);
+ PrintStream out=new PrintStream(file);
+ SimpleDateFormat fmt=new SimpleDateFormat();
+
+ out.println(fmt.format(new Date()));
+ out.close();
+ file.close();
+ }
+
+ public void createDir(String name)
+ throws IOException {
+ File file = new File(name);
+ boolean ok = file.mkdirs();
+ if(! ok)
+ throw new IOException("mkdirs for "+name+" failed");
+ createFile(name);
+ }
+
+ public void handle(InputStream in, OutputStream os) {
+ PrintStream out=null;
+// try {
+// out=new PrintStream(os, true, "US-ASCII"); // Java 1.4+
+// } catch (UnsupportedEncodingException ex) {
+ out=new PrintStream(os, true);
+// }
+
+ while(true) {
+ try {
+ /* If we don't have data in the System InputStream, we want
+ to ask to the user for an option. */
+ if (in.available()==0) {
+ out.println();
+ out.println("Please select one of the following:");
+ out.println(" 1) Shutdown");
+ out.println(" 2) Reload");
+ out.println(" 3) Create a file");
+ out.println(" 4) Disconnect");
+ out.println(" 5) Cause a core of the JVM");
+ out.println(" 6) Create a directory");
+ out.print("Your choice: ");
+ }
+
+ /* Read an option from the client */
+ int x=in.read();
+
+ switch (x) {
+ /* If the socket was closed, we simply return */
+ case -1:
+ return;
+
+ /* Attempt to shutdown */
+ case '1':
+ out.println("Attempting a shutdown...");
+ try {
+ this.controller.shutdown();
+ } catch (IllegalStateException e) {
+ out.println();
+ out.println("Can't shutdown now");
+ e.printStackTrace(out);
+ }
+ break;
+
+ /* Attempt to reload */
+ case '2':
+ out.println("Attempting a reload...");
+ try {
+ this.controller.reload();
+ } catch (IllegalStateException e) {
+ out.println();
+ out.println("Can't reload now");
+ e.printStackTrace(out);
+ }
+ break;
+
+ /* Create a file */
+ case '3':
+ String name=this.getDirectoryName()+
+ "/SimpleDaemon."+
+ this.getConnectionNumber()+
+ ".tmp";
+ try {
+ this.createFile(name);
+ out.println("File '"+name+"' created");
+ } catch (IOException e) {
+ e.printStackTrace(out);
+ }
+ break;
+
+ /* Disconnect */
+ case '4':
+ out.println("Disconnecting...");
+ return;
+
+ /* Crash JVM in a native call: It need an so file ;-) */
+ case '5':
+ System.load(System.getProperty("native.library", "./Native.so"));
+ toto();
+ break;
+
+ /* Create a directory (PR 30177 with 1.4.x and 1.5.0 */
+ case '6':
+ String name1=this.getDirectoryName()+
+ "/a/b/c/d/e"+
+ "/SimpleDaemon."+
+ this.getConnectionNumber()+
+ ".tmp";
+ try {
+ this.createDir(name1);
+ out.println("File '"+name1+"' created");
+ } catch (IOException e) {
+ e.printStackTrace(out);
+ }
+ break;
+
+
+ /* Discard any carriage return / newline characters */
+ case '\r':
+ case '\n':
+ break;
+
+ /* We got something that we weren't supposed to get */
+ default:
+ out.println("Unknown option '"+(char)x+"'");
+ break;
+
+ }
+
+ /* If we get an IOException we return (disconnect) */
+ } catch (IOException e) {
+ System.err.println("SimpleDaemon: IOException in "+
+ "connection "+
+ this.getConnectionNumber());
+ return;
+ }
+ }
+ }
+ }
+}
Added: tomee/deps/branches/commons-daemon/src/samples/SimpleDaemon.sh
URL: http://svn.apache.org/viewvc/tomee/deps/branches/commons-daemon/src/samples/SimpleDaemon.sh?rev=1860225&view=auto
==============================================================================
--- tomee/deps/branches/commons-daemon/src/samples/SimpleDaemon.sh (added)
+++ tomee/deps/branches/commons-daemon/src/samples/SimpleDaemon.sh Tue May 28 09:46:53 2019
@@ -0,0 +1,68 @@
+#!/bin/sh
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# 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.
+#
+# Small shell script to show how to start the sample services.
+#
+# Adapt the following lines to your configuration
+JAVA_HOME=`echo $JAVA_HOME`
+#JAVA_HOME=/opt/java
+#JAVA_HOME=/opt/kaffe
+DAEMON_HOME=`(cd ../..; pwd)`
+TOMCAT_USER=`echo $USER`
+CLASSPATH=\
+$DAEMON_HOME/dist/commons-daemon.jar:\
+$DAEMON_HOME/dist/SimpleDaemon.jar
+
+PATH=$PATH:$DAEMON_HOME/src/native/unix
+export PATH
+
+# library could be used to test restart after a core.
+# -Dnative.library=${DAEMON_HOME}/src/samples/Native.so \
+
+# options below are for kaffe.
+# -Xclasspath/a:$CLASSPATH \
+# (to debug the class loader
+# -vmdebug VMCLASSLOADER \
+
+# option below is for the sun JVM.
+# -cp $CLASSPATH \
+
+if [ -f $JAVA_HOME/bin/kaffe ]
+then
+ CLOPT="-Xclasspath/a:$CLASSPATH"
+else
+ CLOPT="-cp $CLASSPATH"
+fi
+
+jsvc \
+ -user $TOMCAT_USER \
+ -home $JAVA_HOME \
+ $CLOPT \
+ -pidfile ./pidfile \
+ -wait 90 \
+ -debug \
+ -outfile toto.txt \
+ -errfile '&1' \
+ -Dnative.library=${DAEMON_HOME}/src/samples/Native.so \
+ SimpleDaemon \
+#
+# To get a verbose JVM (sun JVM for example)
+#-verbose \
+# To get a debug of jsvc.
+#-debug \
+
+echo "result: $?"
Propchange: tomee/deps/branches/commons-daemon/src/samples/SimpleDaemon.sh
------------------------------------------------------------------------------
svn:executable = *