Showing posts with label maven. Show all posts
Showing posts with label maven. Show all posts

Monday, July 7, 2014

OSGI servlet bridge sample


Servlet bridge is a mechanism for extending your servlet functionality adding other servlets. In this case your main servlet is not affected within some changes however the number of processed request is increased by newly added servlets (modules).
We don't want to have some sophisticated way to register/unregister new modules, don't want to be forced to rebuild the whole application when the new servlet is registered. OSGI is a keyword which helps us to avoid of this issues. In the scope of Servlet Bridge feature OSGI is a powerful mechanism which can help you to extend the functionality of your java servlet application. It can be achieved by using HttpService implementation provided by OSGI standards. Image source: http://www.jayway.com/2007/05/01/osgi-not-just-a-four-letter-word/
(image from: http://www.jayway.com/2007/05/01/osgi-not-just-a-four-letter-word/)
This post is a good way to start with theoretical basics of OSGI, let's try to implement the OSGI servlet bridge.

Tuesday, December 17, 2013

Using maven to create executable jar file

 To create executable jar file with maven all you need to do is to add the corresponding plugin description to your pom.xml file. Shade plugin is what we need!

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.

plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>com.vbashur.trying.threads.App</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>

Monday, December 3, 2012

Read file content with Java (+ Maven )

Simple way to get the file content using java, get it now:

1. Standard Java way to read the file content. There are two methods for different Java versions. readFileJav6 is appropriate for Java 7 as well, but I'd like to recommend you to improve it with new try-with-resources block (see more: http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)

public List<String> readFileJava7(String filename) {
 List<String> content = new LinkedList<String>();
 Path filePath = Paths.get(filename);
 try {
  content = Files.readAllLines(filePath, Charset.defaultCharset());
  //OR
  // String contents = new String(Files.readAllBytes(
Paths.get("alice.txt")), StandardCharsets.UTF_8);
 } catch (IOException e) {
  System.out.println("Unable to read the file " + filename);
 }
 return content;
}

public List<String> readFileJava6(String filename) {
 List<String> content = new LinkedList<String>();
 FileReader fr;
 BufferedReader br = null;
 try {
  fr = new FileReader(filename);
  br = new BufferedReader(fr);
  String output;
  while ((output = br.readLine()) != null) {
   content.add(output);
  }
 } catch (FileNotFoundException e) {
  System.out.println("Unable to find the file " + filename);
 } catch (IOException e) {
  System.out.println("Unable to read the file " + filename);
 } finally {
  try {
   br.close();
  } catch (IOException e) {
   System.out.println("Couldn't close the file " + filename);
  }
 }
 return content;
}

2. Apache Commons IO

String content = IOUtils.toString(new FileReader("file.txt"), "utf-8");


<dependency>
 <groupId>org.apache.commons</groupId>
 <artifactId>commons-io</artifactId>
 <version>1.3.2</version>
</dependency>


 

3. Google Guava

String content = Files.toString(new File("file.txt"), Charsets.UTF_8);


<dependency>
 <groupId>com.google.guava</groupId>
 <artifactId>guava</artifactId>
 <version>r05</version>
</dependency>