-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.clj
179 lines (150 loc) · 4.01 KB
/
system.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
;;;;
;;;; Copyright (c) Ludger Solbach. All rights reserved.
;;;;
;;;; The use and distribution terms for this software are covered by the
;;;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;;;; which can be found in the file license.txt at the root of this distribution.
;;;; By using this software in any fashion, you are agreeing to be bound by
;;;; the terms of this license.
;;;;
;;;; You must not remove this notice, or any other, from this software.
;;;;
(ns org.soulspace.clj.java.system
"Functions for interacting with java.lang.System."
(:require [clojure.string :as str]))
;;;;
;;;; Functions for interacting with java.lang.System
;;;;
;;
;; Environment vars and system properties
;;
(defn get-environment-variable
"Returns the environment variable named var.
If a default is specified and the environment variable is not set, the default will be returned."
([var]
(System/getenv (name var)))
([var default]
(let [env (get-environment-variable var)]
(if (seq? env)
env
default))))
(defn set-system-property
"Sets a system property."
[property value]
(System/setProperty property value))
(defn get-system-property
"Gets a system property.
If a default is specified and the property is not set, the default will be returned."
([property]
(System/getProperty property))
([property default]
(System/getProperty property default)))
(defn clear-system-property
"Removes the system propery."
[property]
(System/clearProperty property))
;;
;; Proxies
;;
(defn use-system-proxies
"Tell the JVM that the system proxies should be used."
[]
(set-system-property "java.net.useSystemProxies" "true"))
(defn set-http-proxy
"Set an HTTP proxy for the JVM."
([host port]
(set-system-property "http.proxyHost" host)
(set-system-property "http.proxyPort" port))
([host port bypassed-hosts]
(set-http-proxy host port)
(set-system-property "http.nonProxyHosts" (str/join "|" bypassed-hosts))))
(defn set-https-proxy
"Set an HTTPS proxy for the JVM."
[host port]
(set-system-property "https.proxyHost" host)
(set-system-property "https.proxyPort" port))
(defn set-ftp-proxy
"Set a FTP proxy for the JVM."
([host port]
(set-system-property "ftp.proxyHost" host)
(set-system-property "ftp.proxyPort" port))
([host port bypassed-hosts]
(set-https-proxy host port)
(set-system-property "ftp.nonProxyHosts" (str/join "|" bypassed-hosts))))
(defn set-socks-proxy
"Set an SOCKS proxy for the JVM."
[host port]
(set-system-property "socksProxyHost" host)
(set-system-property "socksProxyPort" port))
;;
;; Current time
;;
(defn nano-time
"Returns the current time in nano seconds."
[]
(System/nanoTime))
(defn milli-time
"Returns the current time in milli seconds."
[]
(System/currentTimeMillis))
;;
;; Operating systems
;;
(defn os-name
"Returns the value of the os.name property."
[]
(get-system-property "os.name"))
(defn os-windows?
"Tests for Windows OS."
([]
(os-windows? (os-name)))
([os]
(str/starts-with? os "Windows")))
(defn os-linux?
"Tests for Linux OS."
([]
(os-linux? (os-name)))
([os]
(str/starts-with? os "Linux")))
(defn os-mac-os?
"Tests for Mac OS."
([]
(os-mac-os? (os-name)))
([os]
(str/starts-with? os "Mac OS")))
(defn os-sun-os?
"Tests for SunOS."
([]
(os-sun-os? (os-name)))
([os]
(str/starts-with? os "SunOS")))
(defn os-freebsd?
"Tests for FreeBSD OS."
([]
(os-freebsd? (os-name)))
([os]
(str/starts-with? os "FreeBSD")))
(defn os-aix?
"Tests for AIX OS."
([]
(os-aix? (os-name)))
([os]
(str/starts-with? os "AIX")))
(defn os-unix?
"Tests for a Unix OS."
[]
(let [os (os-name)]
(or (os-linux? os) (os-mac-os? os) (os-sun-os? os) (os-freebsd? os) (os-aix? os))))
(defn line-separator
"Returns the line separator of the current system."
[]
(System/lineSeparator))
;;
;; Exit JVM
;;
(defn exit
"Terminates the currently running JVM."
([]
(exit 0))
([status]
(System/exit status)))