Donnerstag, 19. März 2015

In this post I will show how to install CloundFoundry on Windows and VirtualBox using Vagrant and bosh-lite.

Prerequisites:

  1. After you've installed Vagrant and VirtualBox you start by cloning https://github.com/cloudfoundry/bosh-lite

    git clone https://github.com/cloudfoundry/bosh-lite

  2. Now go into your bosh-lite directory and start Vagrant

    vagrant up --provider=virtualbox

  3. Since the bosh-cli doesn't run on Windows we will manage the bosh-lite instance inside the created vm. For that we login into the vm via ssh

    vagrant ssh

  4. Now we need to download a bosh stemcell with the bosh cli tool

    bosh download public stemcell bosh-stemcell-389-warden-boshlite-ubuntu-trusty-go_agent.tgz

  5. After it finished you need to upload the stemcell to your bosh instance

    bosh upload stemcell bosh-stemcell-389-warden-boshlite-ubuntu-trusty-go_agent.tgz

  6. Now we need to clone the CloudFoundry repository. Before we can do this we need to install git

    sudo apt-get install git

  7. Clone the CloudFoundry repository https://github.com/cloudfoundry/cf-release

    git clone https://github.com/cloudfoundry/cf-release

  8. We will use the 1.93 release of CloudFoundry.

    git checkout tags/v193
    ./update to fetch all references

  9. Upload the release to bosh

    bosh upload release releases/cf-193.yml

  10. To use the tool spiff for creating the bosh manifest file we need to install go

    wget https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz
    tar -C /usr/local -xzf go1.4.2.linux-amd64.tar.gz
    export PATH=$PATH:/usr/local/go/bin
    export GO_PATH=$HOME/go
    mkdir $HOME/go

  11. Install spiff

    go get -v github.com/cloudfoundry-incubator/spiff

  12. Build manifest

    Execute inside your bosh-lite folder sudo ./bin/make_manifest_spiff

  13. Choose the deployment

    bosh deployment manifests/cf-manifest.yml

  14. Start deployment of CloudFoundry inside bosh

    bosh deploy

  15. Install the CloudFoundry CLI for Windows on your Windows machine and access your installed CloudFoundry instance

Freitag, 13. März 2015

In the last project where I had to use EclipseLink as the JPA provider for a web-based application. One of the nice features of EclipseLink is the ability of creating dynamic entities. Dynamic entities are virtual entities which can be mapped to the database. That means that you can define a entity at runtime and persist this entity. In this post I will show you how to create dynamic entities and what is needed to use this functionality in a Java EE application. At first you need to create an EntityManager for the defined persistence unit in your persistence.xml. After that you have to create a ClassLoader specifically for the dynamic entity classes by getting the Session and doing a lookup with that Session which returns the DynamicClassLoader. For creating the classes and mapping to the database you have to instantiate the JPADynamicHelper with the created EntityManager.
public void prepare() {
    entityManagerFactory = Persistence.createEntityManagerFactory("DefaultPU");
    entityManager = entityManagerFactory.createEntityManager();
    Session session = JpaHelper.getEntityManager(entityManager).getServerSession();
    dynamicClassLoader = DynamicClassLoader.lookup(session);
    jpaDynamicHelper = new JPADynamicHelper(entityManager);
}
Now you can create dynamically at runtime your first class with createDynamicClass from the DynamicClassLoader. This will return a new Java Class which you have to wrapp into a JPADynamicTypeBuilder. This class allows you to set the different fields and types of your entity and build in the end your dynamic entity class. After you defined your entity class your call addTypes from the JPADynamicHelper which than creates the table.
Class<?> dynamicEntityClass = dynamicClassLoader.createDynamicClass("org.demo.entity.DemoDynamicEntity");
JPADynamicTypeBuilder newType = new JPADynamicTypeBuilder(dynamicEntityClass, null, "DEMO_DYNAMIC_ENTITY");
newType.setPrimaryKeyFields("ID");
newType.addDirectMapping("name", String.class, "NAME");
newType.addDirectMapping("description", String.class, "DESCRIPTION");
jpaDynamicHelper.addTypes(true, true, newType.getType());
To instantiate and persist a dynamic entity you have to call newDynamicEntity from the JPADynamicHelper with the name of your defined class. This method will return you an object of type DynamicEntit which is a wrapper for your virtual classes. This class provides you setter and getter for the fields of your class. After you have finished setting the fields you can persist as usual by calling persist on the EntityManager.
DynamicEntity newDynamicEntity = jpaDynamicHelper.newDynamicEntity("org.demo.entity.DemoDynamicEntity");
newDynamicEntity.set("id", 1);
newDynamicEntity.set("name", "Demo");
newDynamicEntity.set("description", "Demo Entity");
entityManager.persist(newDynamicEntity);

Montag, 2. März 2015

OpenHAB Metro like Visualization

Currently I'am trying to make my home smarter. I'am a big fan of home automation and wanted a flexible gateway for using different protocols and sensors. Therefore I've started with the OpenHAB project which is a good choice if you want to connect many smarthome protocols. In my setup I'am using Z-Wave dongle and some of the Fibaro Dimmers and Shutter Modules. Furthermore I also have Philips HUE Bridge and a Bluetooth Dongle connected to the server where OpenHAB is running. The Bluetooth Dongle is used for receiving weather data from an Arduino running Bluetooth Low Energy, a temperature sensor and a wind sensor. Since I like the Metro style from Windows 8 or moreover the simple arrangement of tiles I wanted a visualization which was as simple as the Metro UI. And this visualization should of course be a website which I can access from my tablet as well. Fortunately there is a library/styling for Metro sites on http://metroui.org.ua/. After some playings I've implemented a simple visualization which uses the Metro stylings and the CometVISU on the OpenHAB server for writing and receiving item states.


Furthermore I've added the noUiSlider library for my dimming buttons.

For all of you intereseted in this Visualization I've created a Github repository under:
https://github.com/wulfdj/smarthome_metro

To start using this demonstration you can just clone this repository into your OpenHAB webapps folder and accessing it under http://OPENAHAB-IP:8080/smarthome_metro. But don't forget putting the CometVISU backend jar into your OpenHAB addons folder.

Montag, 23. Februar 2015

In this post I will show how you can integrate Swagger into a Spring RESTful service. For this tutorial I am using the Spring RESTful maven project from this site. At first you have to add the swagger-springmvc and scala-library libraries to the dependencies in your maven pom.xml so that it looks like this:
<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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>de.wulfdj</groupId>
    <artifactId>springrestswagger</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.10.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.mangofactory</groupId>
            <artifactId>swagger-springmvc</artifactId>
            <version>0.9.3</version>
        </dependency>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.10.4</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>
</project>
Now you can annotate your RESTful interfaces with the corresponding Annotations @Api and @ApiMethod.
package hello;

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

@Api(value  ="/", description = "Greeting service")
@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @ApiOperation(value = "/greeting", response = Greeting.class)
    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));
    }
}
Next you have to implement a Configuration class for Swagger where you define the general API informations and define which paths you will include.
package hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;
import com.wordnik.swagger.model.ApiInfo;

@Configuration
@EnableSwagger
public class SwaggerConfiguration {

 private SpringSwaggerConfig springSwaggerConfig;
 
 @Autowired
 public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {
  this.springSwaggerConfig = springSwaggerConfig;
 }
 
 @Bean
 public SwaggerSpringMvcPlugin swaggerSpringMvcPluginFactory() {
  ApiInfo apiInfo = new ApiInfo("Spring RESTful Greeting Service with Swagger integration", "", "", "", "", "");
  return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(apiInfo).includePatterns("/.*");
 }
}
And as a last step you have to add the Annotation @ComponentScan above the main application class to activate this configuration.
package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

 public static void main(String[] args) {
  SpringApplication.run(Application.class, args);
 }
}
After that you can start your application and go to http://localhost:8080/api-docs where you will see the Swagger output.

Sonntag, 22. Februar 2015

Recently I had to implement a RESTful service which had to have a CRUD interface for storing entities with circular dependencies given as JSON. Usually if you have for example an object like this:
"graph": {
          "nodes": [{"name":"n1"}, {"name":"n2"}],
          "transitions": [{"source": {"name":"n1"}, "target": {"name":"n2"}]
}
and having these Java classes:
@Entity
class Graph {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    long id = 0;

    List<Node> nodes = new ArrayList<Node>();
    
    List<Transition> transitions = new ArrayList<Transition>();
}

@Entity
class Node {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    long id = 0;

    String name = "";
}

@Entity
class Transition {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    long id = 0;

    Node source;

    Node target;
}
the nodes would be deserialized into 4 Java objects and therefore storing with different ids in the database. If you want instead referencing inside the JSON string the already defined nodes you have to add the @JsonIdentityInfo above the entities.
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
@Entity
class Node {
     @Id
     @GeneratedValue(strategy=GenerationType.AUTO)
     long id = 0;

     String name = "";
}
After that you can define your references by using the id you used for nodes and the transitions will have references to the correct nodes.
"graph": {
          "nodes": [{"@id":"1", "name":"n1"}, {"@id":"2", "name":"n2"}],
          "transitions": [{"source": "1", "target": "2"]
}

Samstag, 21. Februar 2015

While making my home smart and automated I wanted to see the phonelist from my Fritz!Box 7362SL in my home visualization as well. Since I didn't find a javascript library for accessing the Fritz!Box via a Challenge-response authentication to put directly into my visualization page I later came across a Java implementation on https://github.com/grundid/fritzbox-java-api. This library is currently able to login into a Fritz!Box and modifying the Wifi Guest-Acces. I've added an additional method inside the FritzTemplate class for fetching the phonelist as a CSV stream.
public List<CallEntry> getPhoneList() {
    if (sessionId == null) {
        getSessionId();
    }
    List<CallEntry> result = new ArrayList<CallEntry>();
    ResponseEntity<String> response = restOperations.getForEntity(baseUrl + "/fon_num/foncalls_list.lua?sid={sid}&csv=", String.class, sessionId);
    
    String lines[] = response.getBody().split("\\r?\\n");
    for(int i=2;i <lines.length; i++) {
        String entries[] = lines[i].split(";");
        CallEntry callEntry = new CallEntry(Integer.parseInt(entries[0]), entries[1], entries[2], entries[3], entries[6]);
        result.add(callEntry);
    }
    return result;
}
Because I wanted to fetch the phonelist directly from a html page and I liked how simple a Spring RESTful service was implemented I made a RESTful microservice with Spring Boot. This microservice could be run automatically on startup with when my OpenHAB server start and provide an interface for fetching the phonelist from my Fritz!Box. At first I've implemented a resource representation class CallEntry:
package de.grundid.fritz.entity;

public class CallEntry {

 private String date = "";
 private String name = "";
 private String number = "";
 private String duration = "";
 private int type = 0;
 
 public CallEntry(int type, String date, String name, String number, String duration) {
  this.type = type;
  this.date = date;
  this.name = name;
  this.number = number;
  this.duration = duration;
 }
 
 public String getDuration() {
  return duration;
 }
 
 public void setDuration(String duration) {
  this.duration = duration;
 }
 
 public String getNumber() {
  return number;
 }
 
 public String getName() {
  return name;
 }
 
 public String getDate() {
  return date;
 }

 public int getType() {
  return type;
 }
}

After that I've implemented a controller class which handles the HTTP requests and returns a collection with the resources. The password for my Fritz!Box is stored in a property file named application.properties. This password will be injected into the password field.
package de.grundid.fritz.service;

import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import de.grundid.fritz.FritzTemplate;
import de.grundid.fritz.entity.CallEntry;

@RestController
public class CallListController {

 @Value("${fritz.password}")
 private String password;
 
 @RequestMapping(value = "/calllist/{limit}",method=RequestMethod.GET)
 public Collection<CallEntry> getCallListAsCSV(@PathVariable long limit) {
  RestTemplate restTemplate = new RestTemplate();
  FritzTemplate template = new FritzTemplate(restTemplate, password);
  List<CallEntry> phoneList = template.getPhoneList();
  if (phoneList.size() <= limit) {
   return phoneList;
  }
  return  phoneList.subList(0, (int) (limit - 1));
 }
}
And finally to make this Spring project runnable by its own I've implemented a CallListApp with a main method:
package de.grundid.fritz.service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
@EnableAutoConfiguration
public class CallListApp {

    public static void main(String[] args) {
        SpringApplication.run(CallListApp.class, args);
    }
}
This class can be executed and Spring starts an embedded tomcat server which handles the HTTP requests on port 8080. To access the service from a different domain or different port by a javascript request I needed to activate CORS on the service side. For that I've added a Filter which addss particular header entries into the response.
package de.grundid.fritz.service;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;

@Component
public class SimpleCORSFilter implements Filter {

        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
            HttpServletResponse response = (HttpServletResponse) res;
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
            response.setHeader("Access-Control-Max-Age", "3600");
            response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
            chain.doFilter(req, res);
        }

        public void init(FilterConfig filterConfig) {}

        public void destroy() {}

}
This was it and after that I could enjoy the complete calling list on my Smarthome Visualization page. If you are interested in using this service I've added a zip file with my eclipse project. https://drive.google.com/file/d/0B4uu0M3bXvqkY2pUTzRadUFGc28/view?usp=sharing

Mittwoch, 27. August 2014

Once I needed to implement a small script that should read rows from a database and create a CSV file based on a particular filtering. I knew much about command line VB and Java scripting but a main requirement was to have a simple GUI which for entering the filter criteria.

After a while I ’ve found a tutorial about HTML Applications and immediately liked the way of implementing scripted GUI's. A HTML Application (HTA) is not hing more than a normal HTML page with an additional HTA: APPLICATION tag and the .hta file extension telling windows to execute the file with different permissions. That means that you can access the filesystem, databases or other activex libraries. And one of the really nice thing is that you can even style your page with CSS.

So I ’ve decided to write the requested script as a HTML Application which accesses the database and stores the entries as a CSV file. In the following I will post a shorten version of the script for the sake of simplicity. But before you can execute the script you have to install the ODBC Connector for MySQL. As a special gimmick I ’v e added a javascript implementation of a tabpane from WebFX where I ’ve separated the settings from the general view.



<head>
<title>CSV-Generator</title>
<HTA:APPLICATION 
     APPLICATIONNAME="CSV-Generator"
     SCROLL="no"
  BORDERSTYLE="static"
     SINGLEINSTANCE="yes"
     WINDOWSTATE="normal"
  MAXIMIZEBUTTON="no"
>
</head>
<link type="text/css" rel="stylesheet" href="webfx/css/tab.css" />
<script type="text/javascript" src="webfx/js/tabpane.js"></script>
<style>
 .button {
  background-color:#f5f5f5;
  border:1px solid #dedede;
  border-top:1px solid #eee;
  border-left:1px solid #eee;
 }
</style>
<script>
    window.resizeTo(320, 240);
 
 function onLoad() {
  //Read the ini file and populate the setting text fields
  document.getElementById("txtDatabaseName").value = "testdb";
  document.getElementById("txtDatabaseUser").value = "root";
  document.getElementById("txtDatabasePassword").value  = "password";
  document.getElementById("txtCSVFile").value  = "testcsv.csv";
  document.getElementById("txtOffer").value = "O";
 };
 
 function generateCSVFile() {
  var oFS = new ActiveXObject("Scripting.FileSystemObject");
     var adConnection, adRS;

  adConnection = new ActiveXObject("ADODB.Connection");
  adConnection.Provider = "MSDASQL";
  adConnection.Mode = 1;
  adConnection.CursorLocation = 3;
  adConnection.Open("Driver={MySQL ODBC 3.51 Driver};" + 
   "DATABASE=" + document.getElementById("txtDatabaseName").value + ";" + 
   "SERVER=localhost;", 
   document.getElementById("txtDatabaseUser").value, 
   document.getElementById("txtDatabasePassword").value);
                        
  adRS = new ActiveXObject("ADODB.Recordset");
  adRS.ActiveConnection = adConnection
  adRS.CursorLocation = 3;
  adRS.CursorType = 0;
  adRS.LockType = 1;
  adRS.Source = "Select * from article;";
  adRS.Open;

  
  var csvFileName = document.getElementById("txtCSVFile").value;
  var csvFile = oFS.OpenTextFile(csvFileName, 2, true);

  while(!adRS.EOF) {
   if (adRS.Fields.Item("identification").Value == document.getElementById("txtOffer").value) {
    csvFile.WriteLine(adRS.Fields.Item("ID").Value + "," + adRS.Fields.Item("name").Value)
   }
   adRS.MoveNext;
  }
        csvFile.close();
  adRS.close();
  adConnection.close();
 };
</script>
<body onload="onLoad();">
 <div class="tab-pane" id="tabPane1">
  <script type="text/javascript">tp1 = new WebFXTabPane( document.getElementById( "tabPane1" ) );</script>
  <div class="tab-page" id="tabPage1">
   <h2 class="tab">General</h2>
   <script type="text/javascript">tp1.addTabPage( document.getElementById( "tabPage1" ) );</script>
   <div style="font:11px MS Sans Serif;font-weight:bold;">Identifier in K1</div>
    <table border="0" cellspacing="2" cellpadding="0">
     <tr>
      <td style="font:11px MS Sans Serif;" width="200">Offering</td><td width="60"><input type="text" id="txtOffer" size="2"></td>
     </tr>
    </table>
    <br>
    <input type="button" class="button" value="Generate CSV file" name="run_button"  onClick="generateCSVFile();">
   </div>
  <div class="tab-page" id="tabPage2">
   <h2 class="tab">Settings</h2>
   <script type="text/javascript">tp1.addTabPage( document.getElementById( "tabPage2" ) );</script>
   <table border="0" cellspacing="2" cellpadding="0">
    <tr>
     <td style="font:11px MS Sans Serif;" width="150">Database</td><td width="120"><input type="text" id="txtDatabaseName"></td>
    </tr>
    <tr>
     <td style="font:11px MS Sans Serif;" width="150">User</td><td width="120"><input type="text" id="txtDatabaseUser"></td>
    </tr>
    <tr>
     <td style="font:11px MS Sans Serif;" width="150">Password</td><td width="120"><input type="text" id="txtDatabasePassword"></td>
    </tr>
    <tr>
     <td style="font:11px MS Sans Serif;" width="150">CSV file</td><td width="120"><input type="text" id="txtCSVFile"></td>
    </tr>
   </table>
   <input width="100%" type="button" class="button" value="Save settings" name="run_button"  onClick="saveSettings();"></td>
  </div>
 </div>
</body>