Browse Source
Create a new ApplicationPid class to remove the need for SystemUtils and refactor existing calls.pull/718/head
8 changed files with 243 additions and 229 deletions
@ -0,0 +1,99 @@
@@ -0,0 +1,99 @@
|
||||
/* |
||||
* Copyright 2012-2014 the original author or authors. |
||||
* |
||||
* 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 |
||||
* |
||||
* 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. |
||||
*/ |
||||
|
||||
package org.springframework.boot; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileWriter; |
||||
import java.io.IOException; |
||||
import java.lang.management.ManagementFactory; |
||||
|
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.ObjectUtils; |
||||
|
||||
/** |
||||
* An application process ID. |
||||
* |
||||
* @author Phillip Webb |
||||
*/ |
||||
public class ApplicationPid { |
||||
|
||||
private final String pid; |
||||
|
||||
public ApplicationPid() { |
||||
this.pid = getPid(); |
||||
} |
||||
|
||||
protected ApplicationPid(String pid) { |
||||
this.pid = pid; |
||||
} |
||||
|
||||
private String getPid() { |
||||
try { |
||||
String jvmName = ManagementFactory.getRuntimeMXBean().getName(); |
||||
return jvmName.split("@")[0]; |
||||
} |
||||
catch (Throwable ex) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return (this.pid == null ? "???" : this.pid); |
||||
} |
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
return ObjectUtils.nullSafeHashCode(this.pid); |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object obj) { |
||||
if (obj == this) { |
||||
return true; |
||||
} |
||||
if (obj != null && obj instanceof ApplicationPid) { |
||||
return ObjectUtils.nullSafeEquals(this.pid, ((ApplicationPid) obj).pid); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* Write the PID to the specified file. |
||||
* @throws IllegalStateException if no PID is available. |
||||
* @throws IOException if the file cannot be written |
||||
*/ |
||||
public void write(File file) throws IOException { |
||||
Assert.state(this.pid != null, "No PID available"); |
||||
createParentFolder(file); |
||||
FileWriter writer = new FileWriter(file); |
||||
try { |
||||
writer.append(this.pid); |
||||
} |
||||
finally { |
||||
writer.close(); |
||||
} |
||||
} |
||||
|
||||
private void createParentFolder(File file) { |
||||
File parent = file.getParentFile(); |
||||
if (parent != null) { |
||||
parent.mkdirs(); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -1,63 +0,0 @@
@@ -1,63 +0,0 @@
|
||||
/* |
||||
* Copyright 2010-2014 the original author or authors. |
||||
* |
||||
* 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 |
||||
* |
||||
* 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. |
||||
*/ |
||||
|
||||
package org.springframework.boot.util; |
||||
|
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
import org.springframework.util.StringUtils; |
||||
|
||||
import java.lang.management.ManagementFactory; |
||||
import java.lang.management.RuntimeMXBean; |
||||
|
||||
/** |
||||
* Class containing methods related to system utilities |
||||
* |
||||
* @author Jakub Kubrynski |
||||
*/ |
||||
public class SystemUtils { |
||||
|
||||
private static final Log LOG = LogFactory.getLog(SystemUtils.class); |
||||
|
||||
/** |
||||
* Looks for application PID |
||||
* @return application PID |
||||
* @throws java.lang.IllegalStateException if PID could not be determined |
||||
*/ |
||||
public static String getApplicationPid() { |
||||
String pid = null; |
||||
try { |
||||
RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean(); |
||||
String jvmName = runtimeBean.getName(); |
||||
if (StringUtils.isEmpty(jvmName)) { |
||||
LOG.warn("Cannot get JVM name"); |
||||
} |
||||
if (!jvmName.contains("@")) { |
||||
LOG.warn("JVM name doesn't contain process id"); |
||||
} |
||||
pid = jvmName.split("@")[0]; |
||||
} catch (Throwable e) { |
||||
LOG.warn("Cannot get RuntimeMXBean", e); |
||||
} |
||||
|
||||
if (pid == null) { |
||||
throw new IllegalStateException("Application PID not found"); |
||||
} |
||||
|
||||
return pid; |
||||
} |
||||
|
||||
} |
||||
@ -1,21 +0,0 @@
@@ -1,21 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2014 the original author or authors. |
||||
* |
||||
* 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 |
||||
* |
||||
* 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. |
||||
*/ |
||||
|
||||
/** |
||||
* Utility classes |
||||
*/ |
||||
package org.springframework.boot.util; |
||||
|
||||
@ -0,0 +1,77 @@
@@ -0,0 +1,77 @@
|
||||
/* |
||||
* Copyright 2012-2014 the original author or authors. |
||||
* |
||||
* 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 |
||||
* |
||||
* 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. |
||||
*/ |
||||
|
||||
package org.springframework.boot; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileReader; |
||||
|
||||
import org.junit.Rule; |
||||
import org.junit.Test; |
||||
import org.junit.rules.ExpectedException; |
||||
import org.junit.rules.TemporaryFolder; |
||||
import org.springframework.util.FileCopyUtils; |
||||
|
||||
import static org.hamcrest.Matchers.equalTo; |
||||
import static org.hamcrest.Matchers.isEmptyOrNullString; |
||||
import static org.hamcrest.Matchers.not; |
||||
import static org.junit.Assert.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@link ApplicationPid}. |
||||
* @author Phillip Webb |
||||
*/ |
||||
public class ApplicationPidTests { |
||||
|
||||
@Rule |
||||
public ExpectedException thrown = ExpectedException.none(); |
||||
|
||||
@Rule |
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder(); |
||||
|
||||
@Test |
||||
public void toStringWithPid() throws Exception { |
||||
assertThat(new ApplicationPid("123").toString(), equalTo("123")); |
||||
} |
||||
|
||||
@Test |
||||
public void toStringWithoutPid() throws Exception { |
||||
assertThat(new ApplicationPid(null).toString(), equalTo("???")); |
||||
} |
||||
|
||||
@Test |
||||
public void throwIllegalStateWritingMissingPid() throws Exception { |
||||
ApplicationPid pid = new ApplicationPid(null); |
||||
this.thrown.expect(IllegalStateException.class); |
||||
this.thrown.expectMessage("No PID available"); |
||||
pid.write(this.temporaryFolder.newFile()); |
||||
} |
||||
|
||||
@Test |
||||
public void writePid() throws Exception { |
||||
ApplicationPid pid = new ApplicationPid("123"); |
||||
File file = this.temporaryFolder.newFile(); |
||||
pid.write(file); |
||||
String actual = FileCopyUtils.copyToString(new FileReader(file)); |
||||
assertThat(actual, equalTo("123")); |
||||
} |
||||
|
||||
@Test |
||||
public void getPidFromJvm() throws Exception { |
||||
assertThat(new ApplicationPid().toString(), not(isEmptyOrNullString())); |
||||
} |
||||
|
||||
} |
||||
@ -1,38 +0,0 @@
@@ -1,38 +0,0 @@
|
||||
/* |
||||
* Copyright 2010-2014 the original author or authors. |
||||
* |
||||
* 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 |
||||
* |
||||
* 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. |
||||
*/ |
||||
|
||||
package org.springframework.boot.util; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import static org.junit.Assert.assertNotNull; |
||||
|
||||
/** |
||||
* Tests for {@link org.springframework.boot.util.SystemUtils}. |
||||
* |
||||
* @author Jakub Kubrynski |
||||
*/ |
||||
public class SystemUtilsTest { |
||||
|
||||
@Test |
||||
public void shouldGetApplicationPid() throws Exception { |
||||
//when
|
||||
String applicationPid = SystemUtils.getApplicationPid(); |
||||
|
||||
//then
|
||||
assertNotNull(applicationPid); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue