Merge remote-tracking branch 'eugenp/master'
This commit is contained in:
@@ -48,3 +48,12 @@ dependency-reduced-pom.xml
|
||||
*.so
|
||||
*.dylib
|
||||
*.dll
|
||||
|
||||
xml/src/test/resources/example_dom4j_new.xml
|
||||
xml/src/test/resources/example_dom4j_updated.xml
|
||||
xml/src/test/resources/example_jaxb_new.xml
|
||||
core-java-io/hard_link.txt
|
||||
core-java-io/target_link.txt
|
||||
core-java/src/main/java/com/baeldung/manifest/MANIFEST.MF
|
||||
ethereum/logs/
|
||||
jmeter/src/main/resources/*-JMeter.csv
|
||||
@@ -99,6 +99,16 @@
|
||||
<artifactId>joda-time</artifactId>
|
||||
<version>${joda.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjrt</artifactId>
|
||||
<version>${asspectj.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>${asspectj.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -170,6 +180,7 @@
|
||||
<joda.version>2.10</joda.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<asspectj.version>1.8.9</asspectj.version>
|
||||
<avaitility.version>1.7.0</avaitility.version>
|
||||
<jmh-core.version>1.19</jmh-core.version>
|
||||
<jmh-generator.version>1.19</jmh-generator.version>
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.aspect;
|
||||
|
||||
public aspect ChangeCallsToCurrentTimeInMillisMethod {
|
||||
long around():
|
||||
call(public static native long java.lang.System.currentTimeMillis())
|
||||
&& within(user.code.base.pckg.*) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.baeldung.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
@@ -9,6 +10,8 @@ import java.time.LocalTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.temporal.ChronoField;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CurrentDateTimeUnitTest {
|
||||
@@ -39,5 +42,4 @@ public class CurrentDateTimeUnitTest {
|
||||
|
||||
assertEquals(clock.instant().getEpochSecond(), now.getEpochSecond());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+8
-18
@@ -1,18 +1,19 @@
|
||||
package com.baeldung.convertlisttomap;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.collections4.IterableUtils;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
public class ConvertListToMapService {
|
||||
|
||||
public Map<Integer, Animal> convertListBeforeJava8(List<Animal> list) {
|
||||
Map<Integer, Animal> map = new HashMap<Integer, Animal>();
|
||||
|
||||
Map<Integer, Animal> map = new HashMap<>();
|
||||
|
||||
for (Animal animal : list) {
|
||||
map.put(animal.getId(), animal);
|
||||
}
|
||||
@@ -30,20 +31,9 @@ public class ConvertListToMapService {
|
||||
return map;
|
||||
}
|
||||
|
||||
public Map<Integer, Animal> convertListWithApacheCommons1(List<Animal> list) {
|
||||
public Map<Integer, Animal> convertListWithApacheCommons(List<Animal> list) {
|
||||
|
||||
Map<Integer, Animal> map = new HashMap<Integer, Animal>();
|
||||
|
||||
IterableUtils.forEach(list, animal -> {
|
||||
map.put(animal.getId(), animal);
|
||||
});
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public Map<Integer, Animal> convertListWithApacheCommons2(List<Animal> list) {
|
||||
|
||||
Map<Integer, Animal> map = new HashMap<Integer, Animal>();
|
||||
Map<Integer, Animal> map = new HashMap<>();
|
||||
|
||||
MapUtils.populateMap(map, list, Animal::getId);
|
||||
|
||||
+8
-13
@@ -1,13 +1,14 @@
|
||||
package com.baeldung.convertlisttomap;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
|
||||
public class ConvertListToMapServiceUnitTest {
|
||||
List<Animal> list;
|
||||
@@ -29,6 +30,7 @@ public class ConvertListToMapServiceUnitTest {
|
||||
list.add(cow);
|
||||
Animal goat = new Animal(5, "Goat");
|
||||
list.add(goat);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -56,18 +58,11 @@ public class ConvertListToMapServiceUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenConvertWithApacheCommons1_thenReturnMapWithTheSameElements() {
|
||||
public void givenAList_whenConvertWithApacheCommons_thenReturnMapWithTheSameElements() {
|
||||
|
||||
Map<Integer, Animal> map = convertListService.convertListWithApacheCommons1(list);
|
||||
Map<Integer, Animal> map = convertListService.convertListWithApacheCommons(list);
|
||||
|
||||
assertThat(map.values(), containsInAnyOrder(list.toArray()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenConvertWithApacheCommons2_thenReturnMapWithTheSameElements() {
|
||||
|
||||
Map<Integer, Animal> map = convertListService.convertListWithApacheCommons2(list);
|
||||
|
||||
assertThat(map.values(), containsInAnyOrder(list.toArray()));
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package com.baeldung.convertlisttomap;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
|
||||
public class ConvertListWithDiplicatedIdToMapServiceUnitTest {
|
||||
List<Animal> duplicatedIdList;
|
||||
|
||||
private ConvertListToMapService convertListService = new ConvertListToMapService();
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
|
||||
this.duplicatedIdList = new ArrayList<>();
|
||||
|
||||
Animal cat = new Animal(1, "Cat");
|
||||
duplicatedIdList.add(cat);
|
||||
Animal dog = new Animal(2, "Dog");
|
||||
duplicatedIdList.add(dog);
|
||||
Animal pig = new Animal(3, "Pig");
|
||||
duplicatedIdList.add(pig);
|
||||
Animal cow = new Animal(4, "Cow");
|
||||
duplicatedIdList.add(cow);
|
||||
Animal goat = new Animal(4, "Goat");
|
||||
duplicatedIdList.add(goat);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenADupIdList_whenConvertBeforeJava8_thenReturnMapWithRewrittenElement() {
|
||||
|
||||
Map<Integer, Animal> map = convertListService.convertListBeforeJava8(duplicatedIdList);
|
||||
|
||||
assertThat(map.values(), hasSize(4));
|
||||
assertThat(map.values(), hasItem(duplicatedIdList.get(4)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenADupIdList_whenConvertWithApacheCommons_thenReturnMapWithRewrittenElement() {
|
||||
|
||||
Map<Integer, Animal> map = convertListService.convertListWithApacheCommons(duplicatedIdList);
|
||||
|
||||
assertThat(map.values(), hasSize(4));
|
||||
assertThat(map.values(), hasItem(duplicatedIdList.get(4)));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void givenADupIdList_whenConvertAfterJava8_thenException() {
|
||||
|
||||
convertListService.convertListAfterJava8(duplicatedIdList);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void givenADupIdList_whenConvertWithGuava_thenException() {
|
||||
|
||||
convertListService.convertListWithGuava(duplicatedIdList);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -147,6 +147,17 @@
|
||||
<artifactId>icu4j</artifactId>
|
||||
<version>${icu4j.version}</version>
|
||||
</dependency>
|
||||
<!-- Mime Type Resolution Libraries -->
|
||||
<dependency>
|
||||
<groupId>org.apache.tika</groupId>
|
||||
<artifactId>tika-core</artifactId>
|
||||
<version>${tika.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.sf.jmimemagic</groupId>
|
||||
<artifactId>jmimemagic</artifactId>
|
||||
<version>${jmime-magic.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-dbcp2</artifactId>
|
||||
@@ -162,6 +173,19 @@
|
||||
<artifactId>c3p0</artifactId>
|
||||
<version>${c3p0.version}</version>
|
||||
</dependency>
|
||||
<!-- instrumentation -->
|
||||
<dependency>
|
||||
<groupId>org.javassist</groupId>
|
||||
<artifactId>javassist</artifactId>
|
||||
<version>${javaassist.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun</groupId>
|
||||
<artifactId>tools</artifactId>
|
||||
<version>1.8.0</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>${java.home}/../lib/tools.jar</systemPath>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -389,6 +413,111 @@
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
|
||||
<!-- java instrumentation profiles to build jars -->
|
||||
<profile>
|
||||
<id>buildAgentLoader</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>agentLoader</classifier>
|
||||
<classesDirectory>target/classes</classesDirectory>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
</manifest>
|
||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
||||
</archive>
|
||||
|
||||
<includes>
|
||||
<include>com/baeldung/instrumentation/application/AgentLoader.class</include>
|
||||
<include>com/baeldung/instrumentation/application/Launcher.class</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>buildApplication</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>application</classifier>
|
||||
<classesDirectory>target/classes</classesDirectory>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
</manifest>
|
||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
||||
</archive>
|
||||
|
||||
<includes>
|
||||
<include>com/baeldung/instrumentation/application/MyAtm.class</include>
|
||||
<include>com/baeldung/instrumentation/application/MyAtmApplication.class</include>
|
||||
<include>com/baeldung/instrumentation/application/Launcher.class</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>buildAgent</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>agent</classifier>
|
||||
<classesDirectory>target/classes</classesDirectory>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
</manifest>
|
||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
||||
</archive>
|
||||
|
||||
<includes>
|
||||
<include>com/baeldung/instrumentation/agent/AtmTransformer.class</include>
|
||||
<include>com/baeldung/instrumentation/agent/MyInstrumentationAgent.class</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
@@ -439,6 +568,11 @@
|
||||
<spring-boot-maven-plugin.version>2.0.3.RELEASE</spring-boot-maven-plugin.version>
|
||||
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
|
||||
<icu4j.version>61.1</icu4j.version>
|
||||
<!-- Mime Type Libraries -->
|
||||
<tika.version>1.18</tika.version>
|
||||
<jmime-magic.version>0.1.5</jmime-magic.version>
|
||||
<!-- instrumentation -->
|
||||
<javaassist.version>3.21.0-GA</javaassist.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
+2
-4
@@ -33,7 +33,7 @@ public class BasicConnectionPool implements ConnectionPool {
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
if (connectionPool.size() == 0) {
|
||||
if (connectionPool.isEmpty()) {
|
||||
if (usedConnections.size() < MAX_POOL_SIZE) {
|
||||
connectionPool.add(createConnection(url, user, password));
|
||||
} else {
|
||||
@@ -76,9 +76,7 @@ public class BasicConnectionPool implements ConnectionPool {
|
||||
}
|
||||
|
||||
public void shutdown() throws SQLException {
|
||||
for (Connection c : usedConnections) {
|
||||
this.releaseConnection(c);
|
||||
}
|
||||
usedConnections.forEach(this::releaseConnection);
|
||||
for (Connection c : connectionPool) {
|
||||
c.close();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.instrumentation.agent;
|
||||
|
||||
import javassist.CannotCompileException;
|
||||
import javassist.ClassPool;
|
||||
import javassist.CtClass;
|
||||
import javassist.CtMethod;
|
||||
import javassist.NotFoundException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.instrument.ClassFileTransformer;
|
||||
import java.lang.instrument.IllegalClassFormatException;
|
||||
import java.security.ProtectionDomain;
|
||||
|
||||
public class AtmTransformer implements ClassFileTransformer {
|
||||
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(AtmTransformer.class);
|
||||
|
||||
private static final String WITHDRAW_MONEY_METHOD = "withdrawMoney";
|
||||
|
||||
/** The internal form class name of the class to transform */
|
||||
private String targetClassName;
|
||||
/** The class loader of the class we want to transform */
|
||||
private ClassLoader targetClassLoader;
|
||||
|
||||
public AtmTransformer(String targetClassName, ClassLoader targetClassLoader) {
|
||||
this.targetClassName = targetClassName;
|
||||
this.targetClassLoader = targetClassLoader;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
|
||||
ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
|
||||
byte[] byteCode = classfileBuffer;
|
||||
|
||||
String finalTargetClassName = this.targetClassName.replaceAll("\\.", "/"); //replace . with /
|
||||
if (!className.equals(finalTargetClassName)) {
|
||||
return byteCode;
|
||||
}
|
||||
|
||||
if (className.equals(finalTargetClassName) && loader.equals(targetClassLoader)) {
|
||||
LOGGER.info("[Agent] Transforming class MyAtm");
|
||||
try {
|
||||
ClassPool cp = ClassPool.getDefault();
|
||||
CtClass cc = cp.get(targetClassName);
|
||||
CtMethod m = cc.getDeclaredMethod(WITHDRAW_MONEY_METHOD);
|
||||
m.addLocalVariable("startTime", CtClass.longType);
|
||||
m.insertBefore("startTime = System.currentTimeMillis();");
|
||||
|
||||
StringBuilder endBlock = new StringBuilder();
|
||||
|
||||
m.addLocalVariable("endTime", CtClass.longType);
|
||||
m.addLocalVariable("opTime", CtClass.longType);
|
||||
endBlock.append("endTime = System.currentTimeMillis();");
|
||||
endBlock.append("opTime = (endTime-startTime)/1000;");
|
||||
|
||||
endBlock.append("LOGGER.info(\"[Application] Withdrawal operation completed in:\" + opTime + \" seconds!\");");
|
||||
|
||||
m.insertAfter(endBlock.toString());
|
||||
|
||||
byteCode = cc.toBytecode();
|
||||
cc.detach();
|
||||
} catch (NotFoundException | CannotCompileException | IOException e) {
|
||||
LOGGER.error("Exception", e);
|
||||
}
|
||||
}
|
||||
return byteCode;
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.instrumentation.agent;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.instrument.Instrumentation;
|
||||
|
||||
public class MyInstrumentationAgent {
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(MyInstrumentationAgent.class);
|
||||
|
||||
public static void premain(String agentArgs, Instrumentation inst) {
|
||||
LOGGER.info("[Agent] In premain method");
|
||||
|
||||
String className = "com.baeldung.instrumentation.application.MyAtm";
|
||||
transformClass(className,inst);
|
||||
}
|
||||
|
||||
public static void agentmain(String agentArgs, Instrumentation inst) {
|
||||
LOGGER.info("[Agent] In agentmain method");
|
||||
|
||||
String className = "com.baeldung.instrumentation.application.MyAtm";
|
||||
transformClass(className,inst);
|
||||
}
|
||||
|
||||
private static void transformClass(String className, Instrumentation instrumentation) {
|
||||
Class<?> targetCls = null;
|
||||
ClassLoader targetClassLoader = null;
|
||||
// see if we can get the class using forName
|
||||
try {
|
||||
targetCls = Class.forName(className);
|
||||
targetClassLoader = targetCls.getClassLoader();
|
||||
transform(targetCls, targetClassLoader, instrumentation);
|
||||
return;
|
||||
} catch (Exception ex) {
|
||||
LOGGER.error("Class [{}] not found with Class.forName");
|
||||
}
|
||||
// otherwise iterate all loaded classes and find what we want
|
||||
for(Class<?> clazz: instrumentation.getAllLoadedClasses()) {
|
||||
if(clazz.getName().equals(className)) {
|
||||
targetCls = clazz;
|
||||
targetClassLoader = targetCls.getClassLoader();
|
||||
transform(targetCls, targetClassLoader, instrumentation);
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("Failed to find class [" + className + "]");
|
||||
}
|
||||
|
||||
private static void transform(Class<?> clazz, ClassLoader classLoader, Instrumentation instrumentation) {
|
||||
AtmTransformer dt = new AtmTransformer(clazz.getName(), classLoader);
|
||||
instrumentation.addTransformer(dt, true);
|
||||
try {
|
||||
instrumentation.retransformClasses(clazz);
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException("Transform failed for class: [" + clazz.getName() + "]", ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.instrumentation.application;
|
||||
|
||||
import com.sun.tools.attach.VirtualMachine;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Created by adi on 6/10/18.
|
||||
*/
|
||||
public class AgentLoader {
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(AgentLoader.class);
|
||||
|
||||
public static void run(String[] args) {
|
||||
String agentFilePath = "/home/adi/Desktop/agent-1.0.0-jar-with-dependencies.jar";
|
||||
String applicationName = "MyAtmApplication";
|
||||
|
||||
//iterate all jvms and get the first one that matches our application name
|
||||
Optional<String> jvmProcessOpt = Optional.ofNullable(VirtualMachine.list()
|
||||
.stream()
|
||||
.filter(jvm -> {
|
||||
LOGGER.info("jvm:{}", jvm.displayName());
|
||||
return jvm.displayName().contains(applicationName);
|
||||
})
|
||||
.findFirst().get().id());
|
||||
|
||||
if(!jvmProcessOpt.isPresent()) {
|
||||
LOGGER.error("Target Application not found");
|
||||
return;
|
||||
}
|
||||
File agentFile = new File(agentFilePath);
|
||||
try {
|
||||
String jvmPid = jvmProcessOpt.get();
|
||||
LOGGER.info("Attaching to target JVM with PID: " + jvmPid);
|
||||
VirtualMachine jvm = VirtualMachine.attach(jvmPid);
|
||||
jvm.loadAgent(agentFile.getAbsolutePath());
|
||||
jvm.detach();
|
||||
LOGGER.info("Attached to target JVM and loaded Java agent successfully");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.instrumentation.application;
|
||||
|
||||
/**
|
||||
* Created by adi on 6/14/18.
|
||||
*/
|
||||
public class Launcher {
|
||||
public static void main(String[] args) throws Exception {
|
||||
if(args[0].equals("StartMyAtmApplication")) {
|
||||
new MyAtmApplication().run(args);
|
||||
} else if(args[0].equals("LoadAgent")) {
|
||||
new AgentLoader().run(args);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.instrumentation.application;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Created by adi on 6/11/18.
|
||||
*/
|
||||
public class MyAtm {
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(MyAtm.class);
|
||||
|
||||
private static final int account = 10;
|
||||
|
||||
public static void withdrawMoney(int amount) throws InterruptedException {
|
||||
Thread.sleep(2000l); //processing going on here
|
||||
LOGGER.info("[Application] Successful Withdrawal of [{}] units!", amount);
|
||||
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.instrumentation.application;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class MyAtmApplication {
|
||||
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(MyAtmApplication.class);
|
||||
|
||||
public static void run(String[] args) throws Exception {
|
||||
LOGGER.info("[Application] Starting ATM application");
|
||||
MyAtm.withdrawMoney(Integer.parseInt(args[2]));
|
||||
|
||||
Thread.sleep(Long.valueOf(args[1]));
|
||||
|
||||
MyAtm.withdrawMoney(Integer.parseInt(args[3]));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
Agent-Class: com.baeldung.instrumentation.agent.MyInstrumentationAgent
|
||||
Can-Redefine-Classes: true
|
||||
Can-Retransform-Classes: true
|
||||
Premain-Class: com.baeldung.instrumentation.agent.MyInstrumentationAgent
|
||||
Main-Class: com.baeldung.instrumentation.application.Launcher
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level - %msg%n"/>
|
||||
</Console>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="debug">
|
||||
<AppenderRef ref="Console"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 54 KiB |
@@ -0,0 +1,131 @@
|
||||
package com.baeldung.java.mimetype;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.FileNameMap;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import javax.activation.MimetypesFileTypeMap;
|
||||
|
||||
import org.apache.tika.Tika;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.sf.jmimemagic.Magic;
|
||||
import net.sf.jmimemagic.MagicException;
|
||||
import net.sf.jmimemagic.MagicMatch;
|
||||
import net.sf.jmimemagic.MagicMatchNotFoundException;
|
||||
import net.sf.jmimemagic.MagicParseException;
|
||||
|
||||
/**
|
||||
* Test class demonstrating various strategies to resolve MIME type of a file.
|
||||
* @author tritty
|
||||
*
|
||||
*/
|
||||
public class MimeTypeUnitTest {
|
||||
/**
|
||||
* Expected Ouput.
|
||||
*/
|
||||
public static final String PNG_EXT = "image/png";
|
||||
|
||||
/**
|
||||
* The location of the file.
|
||||
*/
|
||||
public static final String FILE_LOC = "src/test/resources/product.png";
|
||||
|
||||
/**
|
||||
* Test method, demonstrating usage in Java 7.
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void whenUsingJava7_thenSuccess() throws IOException {
|
||||
final Path path = new File(FILE_LOC).toPath();
|
||||
final String mimeType = Files.probeContentType(path);
|
||||
assertEquals(mimeType, PNG_EXT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method demonstrating the usage of URLConnection to resolve MIME type.
|
||||
*
|
||||
* @throws MalformedURLException
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void whenUsingGetContentType_thenSuccess() throws MalformedURLException, IOException {
|
||||
final File file = new File(FILE_LOC);
|
||||
final URLConnection connection = file.toURL()
|
||||
.openConnection();
|
||||
final String mimeType = connection.getContentType();
|
||||
assertEquals(mimeType, PNG_EXT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method demonstrating the usage of URLConnection to resolve MIME type.
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void whenUsingGuessContentTypeFromName_thenSuccess() {
|
||||
final File file = new File(FILE_LOC);
|
||||
final String mimeType = URLConnection.guessContentTypeFromName(file.getName());
|
||||
assertEquals(mimeType, PNG_EXT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method demonstrating the usage of FileNameMap from URLConnection
|
||||
* to resolve MIME type of a file.
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void whenUsingGetFileNameMap_thenSuccess() {
|
||||
final File file = new File(FILE_LOC);
|
||||
final FileNameMap fileNameMap = URLConnection.getFileNameMap();
|
||||
final String mimeType = fileNameMap.getContentTypeFor(file.getName());
|
||||
assertEquals(mimeType, PNG_EXT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method demonstrating the usage of MimeTypesFileTypeMap for resolution of
|
||||
* MIME type.
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void whenUsingMimeTypesFileTypeMap_thenSuccess() {
|
||||
final File file = new File(FILE_LOC);
|
||||
final MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
|
||||
final String mimeType = fileTypeMap.getContentType(file.getName());
|
||||
assertEquals(mimeType, PNG_EXT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method demonstrating usage of jMimeMagic.
|
||||
*
|
||||
* @throws MagicParseException
|
||||
* @throws MagicMatchNotFoundException
|
||||
* @throws MagicException
|
||||
*/
|
||||
@Test
|
||||
public void whenUsingJmimeMagic_thenSuccess() throws MagicParseException, MagicMatchNotFoundException, MagicException {
|
||||
final File file = new File(FILE_LOC);
|
||||
final Magic magic = new Magic();
|
||||
final MagicMatch match = magic.getMagicMatch(file, false);
|
||||
assertEquals(match.getMimeType(), PNG_EXT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method demonstrating usage of Apache Tika.
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void whenUsingTika_thenSuccess() throws IOException {
|
||||
final File file = new File(FILE_LOC);
|
||||
final Tika tika = new Tika();
|
||||
final String mimeType = tika.detect(file);
|
||||
assertEquals(mimeType, PNG_EXT);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Binary file not shown.
|
After Width: | Height: | Size: 54 KiB |
@@ -347,7 +347,7 @@
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<!-- <plugin>
|
||||
<groupId>com.github.ekryd.sortpom</groupId>
|
||||
<artifactId>sortpom-maven-plugin</artifactId>
|
||||
<version>${sortpom-maven-plugin.version}</version>
|
||||
@@ -367,7 +367,7 @@
|
||||
<keepBlankLines>true</keepBlankLines>
|
||||
<expandEmptyElements>false</expandEmptyElements>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugin> -->
|
||||
<plugin>
|
||||
<groupId>com.spotify</groupId>
|
||||
<artifactId>docker-maven-plugin</artifactId>
|
||||
|
||||
@@ -433,6 +433,7 @@
|
||||
<module>spring-data-couchbase-2</module>
|
||||
<module>persistence-modules/spring-data-dynamodb</module>
|
||||
<module>spring-data-elasticsearch</module>
|
||||
<module>spring-data-jpa</module>
|
||||
<module>spring-data-keyvalue</module>
|
||||
<module>spring-data-mongodb</module>
|
||||
<module>persistence-modules/spring-data-neo4j</module>
|
||||
@@ -545,8 +546,9 @@
|
||||
<module>antlr</module>
|
||||
<module>maven-archetype</module>
|
||||
<module>apache-meecrowave</module>
|
||||
<module>spring-reactive-kotlin</module>
|
||||
<module>spring-reactive-kotlin</module>
|
||||
<module>jnosql</module>
|
||||
<module>testing-modules/junit-abstract</module>
|
||||
</modules>
|
||||
|
||||
</profile>
|
||||
@@ -666,6 +668,7 @@
|
||||
<module>spring-amqp-simple</module>
|
||||
<module>spring-apache-camel</module>
|
||||
<module>spring-batch</module>
|
||||
<module>testing-modules/junit-abstract</module>
|
||||
|
||||
|
||||
<!-- group 2 - Pass, 11-16 min, 42 test failures, 4,020 KB -->
|
||||
@@ -1071,7 +1074,7 @@
|
||||
<module>antlr</module>
|
||||
<module>maven-archetype</module>
|
||||
<module>apache-meecrowave</module>
|
||||
|
||||
<module>testing-modules/junit-abstract</module>
|
||||
|
||||
<!-- problematic -->
|
||||
<!--
|
||||
@@ -1220,4 +1223,4 @@
|
||||
<maven-pmd-plugin.version>3.8</maven-pmd-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-data-jpa</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
public static void main(String[] args) {
|
||||
applicationContext = SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
||||
+1
@@ -1,6 +1,7 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import com.baeldung.domain.Article;
|
||||
import com.baeldung.repository.ArticleRepository;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -0,0 +1,15 @@
|
||||
# spring.datasource.x
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=sa
|
||||
|
||||
# hibernate.X
|
||||
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||
hibernate.show_sql=true
|
||||
hibernate.hbm2ddl.auto=create-drop
|
||||
hibernate.cache.use_second_level_cache=true
|
||||
hibernate.cache.use_query_cache=true
|
||||
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
|
||||
|
||||
spring.datasource.data=import_articles.sql
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user