Translate

Saturday, September 15, 2012

Abuse Cases (From Secure Software Development)


Abuse Cases

Gary McGraw describes several best practices for building secure software. One is the use of so-calledabuse cases. Since his chapter on abuse cases left me hungry for more information, this post examines additional literature on the subject and how to fit abuse cases into a Security Development Lifecycle(SDL).

Modeling Functional Requirements With Use Cases

Abuse cases are an adaptation of use cases, abstract episodes of interaction between a system and its environment.
use case consists of a number of related scenarios. A scenario is a description of a specific interaction between the system and particular actors. Each use case has a main success scenario and some additional scenarios to cover variations and exceptional cases.
Actors are external agents, and can be either human or non-human.
For better understanding, each use case should state the goal that the primary actor is working towards.
Use cases are represented inUML diagrams (see example on left) as ovals that are connected to stick figures, which represent the actors. Use case diagrams are accompanied by textual use case descriptions that explain how the actors and the system interact.

Modeling Security Requirements With Abuse Cases

An abuse case is a use case where the results of the interaction are harmful to the system, one of the actors, or one of the stakeholders in the system. An interaction is harmful if it decreases the security (confidentiality, integrity, or availability) of the system.
Abuse cases are also referred to as misuse cases, although some people maintain they’re different. I think the two concepts are too similar to treat differently, so whenever I write “abuse case”, it refers to “misuse case” as well.
Some actors in regular use cases may also act as attacker in an abuse case (e.g. in the case of an insider threat). We should then introduce a new actor to avoid confusion (potentially using inheritance). This is consistent with the best practice of having actors represent roles rather than actual users.
Attackers are described in more detail than regular actors, to make it easier to look at the system from their point of view. Their description should include the resources at their disposal, their skills, and their objectives.
Note that objectives are longer term than the (ab)use case’s goal. For instance, the attacker’s goal for an abuse case may be to gain root privileges on a certain server, while her objective may be industrial espionage.
Abuse cases are very different from use cases in one respect: while we know how the actor in a use case achieves her goal, we don’t know precisely how an attacker will break the system’s security. If we would, we would fix the vulnerability! Therefore, abuse case scenarios describe interactions less precisely than regular use case scenarios.

Modeling Other Non-Functional Requirements

Note that since actors in use cases needn’t be human, we can employ a similar approach to abuse cases with actors like “network failure” etc. to model non-functional requirements beyond security, like reliability, portability, maintainability, etc.
For this to work, one must be able to express the non-functional requirement as an interactive scenario. I won’t go into this topic any further in this post.

Creating Abuse Cases

Abuse case models are best created when use cases are: during requirements gathering. It’s easiest to define the abuse cases after the regular use cases are identified (or even defined).
Abuse case modeling requires one to wear a black hat. Therefore, it makes sense to invite people with black hat capabilities, like testers and network operators or administrators to the table.
The first step in developing abuse cases is to find the actors. As stated before, every actor in a regular use case can potentially be turned into an malicious actor in an abuse case.
We should next add actors for different kinds of intruders. These are distinguished based on their resources and skills.
When we have the actors, we can identify the abuse cases by determining how they might interact with the system. We might identify such malicious interactions by combining the regular use cases with attack patterns.
We can find more abuse cases by combining them systematically and recursively with regular use cases.

Combining Use Cases and Abuse Cases

Some people keep use cases and abuse cases separate to avoid confusion. Others combine them, but display abuse cases as inverted use cases (i.e. black ovals with white text, and actors with black heads).
The latter approach makes it possible to relate abuse cases to use cases using UML associations. For instance, an abuse case maythreaten a use case, while a use case might mitigate an abuse case. The latter use case is also referred to as a security use case. Security use cases usually deal with security features.
Security use cases can be threatened by new abuse cases, for which we can find new security use cases to mitigate, etc. etc. In this way, a “game” of play and counterplay enfolds that fits well in a defense in depth strategy.
We should not expect to win this “game”. Instead, we should make a good trade-off between security requirements and other aspects of the system, like usability and development cost. Ideally, these trade-offs are made clearly visible to stakeholders by using a good risk management framework.

Reusing Abuse Cases

Use cases can be abstracted into essential use cases to make them more reusable. There is no reason we couldn’t do the same with abuse cases and security use cases.
It seems to me that this not just possible, but already done. Microsoft’s STRIDE model contains generalized threats, and its SDL Threat Modeling tool automatically identifies which of those are applicable to your situation.

Conclusion

Although abuse cases are a bit different from regular use cases, their main value is that they present information about security risks in a format that may already be familiar to the stakeholders of the software development process. Developers in particular are likely to know them.
This should make it easier for people with little or no security background to start thinking about securing their systems and how to trade-off security and functionality.
However, it seems that threat modeling gives the same advantages as abuse cases. Since threat modeling is supported by tools, it’s little wonder that people prefer that over abuse cases for inclusion in their Security Development Lifecycle.

Source:http://securesoftwaredev.com/2012/08/13/abuse-cases/

Top 50 Sensor Applications for a Smarter World (Internet of Things)

http://www.libelium.com/top_50_iot_sensor_applications_ranking/

    Smart Cities

  • 01
    Smart Parking
    Monitoring of parking spaces availability in the city.
  • 02
    Structural health
    Monitoring of vibrations and material conditions in buildings, bridges and historical monuments.
  • 03
    Noise Urban Maps
    Sound monitoring in bar areas and centric zones in real time.
  • 04
    Traffic Congestion
    Monitoring of vehicles and pedestrian levels to optimize driving and walking routes.
  • 05
    Smart Lighting
    Intelligent and weather adaptive lighting in street lights.
  • 06
    Waste Management
    Detection of rubbish levels in containers to optimize the trash collection routes.
  • 07
    Intelligent Transportation Systems
    Smart Roads and Intelligent Highways with warning messages and diversions according to climate conditions and unexpected events like accidents or traffic jams.

    Smart Environment

  • 08
    Forest Fire Detection
    Monitoring of combustion gases and preemptive fire conditions to define alert zones.
  • 09
    Air Pollution
    Control of CO2 emissions of factories, pollution emitted by cars and toxic gases generated in farms.
  • 10
    Landslide and Avalanche Prevention
    Monitoring of soil moisture, vibrations and earth density to detect dangerous patterns in land conditions.
  • 11
    Earthquake Early Detection
    Distributed control in specific places of tremors.

    Smart Water

  • 12
    Water Quality
    Study of water suitability in rivers and the sea for fauna and eligibility for drinkable use.
  • 13
    Water Leakages
    Detection of liquid presence outside tanks and pressure variations along pipes.
  • 14
    River Floods
    Monitoring of water level variations in rivers, dams and reservoirs.

    Smart Metering

  • 15
    Smart Grid
    Energy consumption monitoring and management.
  • 16
    Tank level
    Monitoring of water, oil and gas levels in storage tanks and cisterns.
  • 17
    Photovoltaic Installations
    Monitoring and optimization of performance in solar energy plants.
  • 18
    Water Flow
    Measurement of water pressure in water transportation systems.
  • 19
    Silos Stock Calculation
    Measurement of emptiness level and weight of the goods.

    Security & Emergencies

  • 20
    Perimeter Access Control
    Access control to restricted areas and detection of people in non-authorized areas.
  • 21
    Liquid Presence
    Liquid detection in data centers, warehouses and sensitive building grounds to prevent break downs and corrosion.
  • 22
    Radiation Levels
    Distributed measurement of radiation levels in nuclear power stations surroundings to generate leakage alerts.
  • 23
    Explosive and Hazardous Gases
    Detection of gas levels and leakages in industrial environments, surroundings of chemical factories and inside mines.

    Retail

  • 24
    Supply Chain Control
    Monitoring of storage conditions along the supply chain and product tracking for traceability purposes.
  • 25
    NFC Payment
    Payment processing based in location or activity duration for public transport, gyms, theme parks, etc.
  • 26
    Intelligent Shopping Applications
    Getting advices in the point of sale according to customer habits, preferences, presence of allergic components for them or expiring dates.
  • 27
    Smart Product Management
    Control of rotation of products in shelves and warehouses to automate restocking processes.

    Logistics

  • 28
    Quality of Shipment Conditions
    Monitoring of vibrations, strokes, container openings or cold chain maintenance for insurance purposes.
  • 29
    Item Location
    Search of individual items in big surfaces like warehouses or harbours.
  • 30
    Storage Incompatibility Detection
    Warning emission on containers storing inflammable goods closed to others containing explosive material.
  • 31
    Fleet Tracking
    Control of routes followed for delicate goods like medical drugs, jewels or dangerous merchandises.

    Industrial Control

  • 32
    M2M Applications
    Machine auto-diagnosis and assets control.
  • 33
    Indoor Air Quality
    Monitoring of toxic gas and oxygen levels inside chemical plants to ensure workers and goods safety.
  • 34
    Temperature Monitoring
    Control of temperature inside industrial and medical fridges with sensitive merchandise.
  • 35
    Ozone Presence
    Monitoring of ozone levels during the drying meat process in food factories.
  • 36
    Indoor Location
    Asset indoor location by using active (ZigBee) and passive tags (RFID/NFC).
  • 37
    Vehicle Auto-diagnosis
    Information collection from CanBus to send real time alarms to emergencies or provide advice to drivers.

    Smart Agriculture

  • 38
    Wine Quality Enhancing
    Monitoring soil moisture and trunk diameter in vineyards to control the amount of sugar in grapes and grapevine health.
  • 39
    Green Houses
    Control micro-climate conditions to maximize the production of fruits and vegetables and its quality.
  • 40
    Golf Courses
    Selective irrigation in dry zones to reduce the water resources required in the green.
  • 41
    Meteorological Station Network
    Study of weather conditions in fields to forecast ice formation, rain, drought, snow or wind changes.
  • 42
    Compost
    Control of humidity and temperature levels in alfalfa, hay, straw, etc. to prevent fungus and other microbial contaminants.

    Smart Animal Farming

  • 43
    Offspring Care
    Control of growing conditions of the offspring in animal farms to ensure its survival and health.
  • 44
    Animal Tracking
    Location and identification of animals grazing in open pastures or location in big stables.
  • 45
    Toxic Gas Levels
    Study of ventilation and air quality in farms and detection of harmful gases from excrements.

    Domotic & Home Automation

  • 46
    Energy and Water Use
    Energy and water supply consumption monitoring to obtain advice on how to save cost and resources.
  • 47
    Remote Control Appliances
    Switching on and off remotely appliances to avoid accidents and save energy.
  • 48
    Intrusion Detection Systems
    Detection of windows and doors openings and violations to prevent intruders.
  • 49
    Art and Goods Preservation
    Monitoring of conditions inside museums and art warehouses.

    eHealth

  • 50
    Fall Detection
    Assistance for elderly or disabled people living independent.
  • 51
    Medical Fridges
    Control of conditions inside freezers storing vaccines, medicines and organic elements.
  • 52
    Sportsmen Care
    Vital signs monitoring in high performance centers and fields.
  • 53
    Patients Surveillance
    Monitoring of conditions of patients inside hospitals and in old people's home.
  • 54
    Ultraviolet Radiation
    Measurement of UV sun rays to warn people not to be exposed in certain hours.

Naming Antipatterns


Source: http://blog.schauderhaft.de/2012/09/02/naming-antipatterns/

Naming Antipatterns

One of these annoying challenges when coding is finding proper names for your classes. There are some tools available making fun of our inability to come up with proper names. But while I enjoy these kind of gags I think there is some serious problem hiding.
The problem is: Classes should be some kind of abstraction. I should only have one abstraction for a single purpose. But if my classes are unique abstractions it should be easy to name them, right?
You just have to look into the average code base to see it isn’t easy. Lets have a look at a couple of common anti patterns.
AbstractAnything: Often you have a possibly large set of classes that share lots of common stuff. In many cases you find an abstract class at the top of their inheritance tree named AbstractWhatever. Obviously that name is technical correct but not very helpfull. After all, the class itself is telling me it is abstract, no need to put it in the name. But apart from being picky about the name a couple of serious design problem come with these classes. They tend to gather functionality common to their subclasses. The problem is: Just because all (or even worse many) of the subclasses need a feature or function it shouldn’t be embedded in their superclass. The features are often mostly independent and therefore should go in separate classes. Lets take the example of an AbstractEditorwhich is intended to be the base class for models for editors in a Swing application. You might find things in anAbstractEditor like
  • a save method, setting the cursor to its wait state before calling an abstract real save method.
  • a boolean property telling you if this editor needs saving
  • the class and id of the entity being edited
  • Some property change handling infrastructure
  • infrastructure code for validating inputs
  • code that handles the process of asking the user if he wants to save changes when he tries to close the editor
and so on. Of course these features depend on each others, but when they are lumped into a single class the dependencies become muddled. If a new need for a feature occurs there is hardly an option anymore but to put it in the same class and after some time the class looks almost like this example. Note that some developers try to hide the application of this anti pattern by renaming the class to BaseSomething. Doesn’t help though.
AnythingDo, AnythingTo, AnythingBs With this antipattern you have a properly named class and lots of very similar classes with various suffixes. These suffixes often are very short and denote different layers of the application. On the boundaries of these layers data gets copied from one object into the other with barely any logic.
The problem with this antipattern is that while there might be valid reasons for these classes to exists the seamingly determinism to construct one out of the other often runs against the purpose of these classes. An example: You might have a Person class which represents a Person in your system. You might also have aPersonHe (He like Hibernate Entity) which is mapped to a database table using Hibernate.
The Person class is intended be used in all the business logic stuff, but since at the boundary to the persistence layer it is just copied over to the Hibernate Entity it has to be handled in the way Hibernate expects things. For example you have to move the complete Person Object around even if you just want to change a single attribute (e.g. the marriage status), because if you just leave fields empty, Hibernate will store these empty fields in the database and you end up with Persons that don’t have any useful property anymore except being married. Although this actually describes reality pretty good in some cases it normally isn’t what you want.
Instead consider a design where in case of a marriage you actually create a Marriage object in your business logic, which does not have any direct relationship inside the database. You would do all kind of checks and logic in your business layer (without having code like)
if (oldPerson.married != newPerson.married && newPerson.married) ...
And only when you store it you put the information from the Marriage into Hibernate Person Entities. There is no MarriageHe or anything.
This kind of design makes for way more expressive code. But developers don’t realize this option and often it is incredibly hard to force it into the existing infrastructur/architecture, because everything assumes there is a 1:1 relationship between Person and PersoneHe and all the other Person classes.
AnythingImpl This one is annoying. And most people actually feel that they do something wrong when they have an interface X and a single implementation XImpl. It is bad because the Impl suffix basically tells us nothing. JavaDoc already tells us its the implementation of the interface, no need to put that fact into the class name. It also suggests there will always be only one implementation. At least I hope you don’t have classes ending in Impl2and Impl3 in your code base. But if you have only one implementation in the first place why do you have an interface? I doesn’t make sense. Lets think hard about what other implementations there are (or might be).
A classical exammple is the PersonDao interface and PersonDaoImpl. Here are some possible implementation alternatives I would come up with:
  • one implementation could use Hibernate to store and retrieve stuff.
  • one implementation could use a map or similar in memory structure to store stuff. Very usefull for testing
  • one implementation might use special Oracle features
Which one is PersonDaoImpl?
And by contrast which one is OraclePersonDaoHibernatePersonDao and InMemoryPersonDao? If nothing else consider ProductionPersonDao to distinguish it from the implementation used for testing.
The next time you have a good class or interface name and you feel like slapping some kind of standard suffix or prefix onto it in order to create the name for another class, think twice. You might be creating a useless class name, or you might be screwing up your software design.

How to make an executable WAR file


Source: http://saltnlight5.blogspot.gr/2012/08/how-to-make-executable-war-file.html

How to make an executable WAR file

If you ever used Jenkin CI server, you have probably seen their super easy one-line instruction to get started: java -jar jenkins.war. Now that's one awesome way to get your users to try out your product!
In this article, I will show you how to make a WAR file self executable just like above. Before I do that, let me tell you a little web app that I made, and then I will show how I converted it into runnable war.

THE REQUIREMENT: TAKING NOTES EFFICIENTLY

I tend take a lot of notes when I do my work. Instead of using the crapy Windows Notepad.exe, I would setup my cygwin .bash_profile like this:
function e() { /cygdrive/C/apps/notepad++.exe $(cygpath -w "$@") ; }
function n() { F="$HOME/notes/$(date "+%Y%m%d-%H%M%S").markdown"; touch $F; e $F; }
I happen to have Notepad++ installed, but you can use just about any text editor you like. With that setup, then anywhere in my terminal I can simply type n to have a GUI editor pop open, and I will have my note file ready to record notes. The file name would have a timestamp date, and I like to record them with simpleMarkdown syntax for easy viewing later. Since I write them in plain text, it's easy and fast to search using grep MYSTUFF ~/notes/*. Once I found it, I can quickly edit it by e ~/notes/20120814-000000.markdown.
This method of taking notes is fast and portable between my Linux and Windows systems (well, I have to choose a different editor between OS but not big deal). Now since I already recorded my notes in markdown, there is no way I can see the pretty result unless I use an online converter. So I thought it would be awesome if I can have a web app that let me record these notes, and yet give me a nice quick Markdown to html view.

INTRODUCING THE WEBNOTEPAD.WAR

To improve my notes taking, I created little web app called webnotepad.war. It's really simply one page web app that record notes in Markdown syntax, preview it, and allow you to search it. It also let you edit old notes too. The whole thing is just a Servlet class, and it doesn't even use JSP.
However, after I have this little web app, it bothers me that I would always need an app server to run it. (I can run mvn tomcat7:run directly from my source project, but it would require me be online to satisfy initial Maven build.) So here is a perfectly example use case that would be super nice to make this war file self executable. In fact I have succeeded in this little experiement. You can download it and try it out yourself with java -jar webnotepad.war, and then browsehttp://localhost:8080. If you get port conflict, try --httpPort=8081.
Of course you continue to deploy webnotepad.war into any app server too.

MAKING WAR FILE EXECUTABLE

Before you want to make war executable, you want to build and package your normal web application first. The trick to make the war file runnable is that you want additionally add a Main-Class in the META-INF/MANIFEST.MF in the war file. And from this we need to load and run an embedded Servlet server with the self war file deployed. I noticed Jenkins uses one called Winstone container server. I was amazed to find that this server only has 300K in size! It support full Servlet 2.5 spec! It can optionally support JSP with Jasper which will cost you up to 3MB in size. That's still a very small price to pay compare to any Servlet container out there!
Making a Main-Class WinstoneMain.java is not hard. However making Maven to package everything is pretty tricky. Now my webnotepad.war doesn't need Jasper, but if you do, you specially want these jars outside of WEB-INF/lib because you don't want your normal app server to load these! Else you will get very odd errors.
In my example I added into WEB-INF/lib-winstone, and then the WinstoneMain would extract these and then load the winstone.Launcher using a custom class loader. Also, we need to use some Winstone options to start a web server properly. I use this webnotepad/pom.xml to package it all up the war file. You can easily use my example because I have create them under a separate profile and you just need add to your web module/pom.xml. In my project demo you would run mvn package -Pdist to generate the executable war file.
NOTE: I noticed Winstone project didn't show much activity since last release since 2008. But I was happy to find that there is a fork project on GoogleCode Winstone that shows activities. I didn't use this for my demo, but it looks promising.

How to write better POJO services


How to write better POJO Services

In Java, you can easily implement some business logic in Plain Old Java Object (POJO) classes, and then able to run them in a fancy server or framework without much hassle. There many server/frameworks, such as JBossAS, Spring or Camel etc, that would allow you to deploy POJO without even hardcoding to their API. Obviously you would get advance features if you willing to couple to their API specifics, but even if you do, you can keep these to minimal by encapsulating your own POJO and their API in a wrapper. By writing and designing your own application as simple POJO as possible, you will have the most flexible ways in choose a framework or server to deploy and run your application. One effective way to write your business logic in these environments is to use Service component. In this article I will share few things I learned in writing Services.

What is a Service?

The word Service is overly used today, and it could mean many things to different people. When I say Service, my definition is a software component that has minimal of life-cycles such as initstartstop, and destroy. You may not need all these stages of life-cycles in every service you write, but you can simply ignore ones that don't apply. When writing large application that intended for long running such as a server component, definining these life-cycles and ensure they are excuted in proper order is crucial!
I will be walking you through a Java demo project that I have prepared. It's very basic and it should run as stand-alone. The only dependency it has is the SLF4Jlogger. If you don't know how to use logger, then simply replace them with System.out.println. However I would strongly encourage you to learn how to use logger effectively during application development though. Also if you want to try out the Spring related demos, then obviously you would need their jars as well.

Writing basic POJO service

You can quickly define a contract of a Service with life-cycles as below in an interface.
package servicedemo;

public interface Service {
    void init();
    void start();
    void stop();
    void destroy();
    boolean isInited();
    boolean isStarted();
}
Developers are free to do what they want in their Service implementation, but you might want to give them an adapter class so that they don't have to re-write same basic logic on each Service. I would provide an abstract service like this:
package servicedemo;

import java.util.concurrent.atomic.*;
import org.slf4j.*;
public abstract class AbstractService implements Service {
    protected Logger logger = LoggerFactory.getLogger(getClass());
    protected AtomicBoolean started = new AtomicBoolean(false);
    protected AtomicBoolean inited = new AtomicBoolean(false);

    public void init() {
        if (!inited.get()) {
            initService();
            inited.set(true);
            logger.debug("{} initialized.", this);
        }
    }

    public void start() {
        // Init service if it has not done so.
        if (!inited.get()) {
            init();
        }
        // Start service now.
        if (!started.get()) {
            startService();
            started.set(true);
            logger.debug("{} started.", this);
        }
    }

    public void stop() {
        if (started.get()) {
            stopService();
            started.set(false);
            logger.debug("{} stopped.", this);
        }
    }

    public void destroy() {
        // Stop service if it is still running.
        if (started.get()) {
            stop();
        }
        // Destroy service now.
        if (inited.get()) {
            destroyService();
            inited.set(false);
            logger.debug("{} destroyed.", this);
        }
    }

    public boolean isStarted() {
        return started.get();
    }

    public boolean isInited() {
        return inited.get();
    }

    @Override
    public String toString() {
            return getClass().getSimpleName() + "[id=" + System.identityHashCode(this) + "]";
    }

    protected void initService() {
    }

    protected void startService() {
    }

    protected void stopService() {
    }

    protected void destroyService() {
    }
}
This abstract class provide the basic of most services needs. It has a logger and states to keep track of the life-cycles. It then delegate new sets of life-cycle methods so subclass can choose to override. Notice that the start() method is checking auto calling init() if it hasn't already done so. Same is done indestroy() method to the stop() method. This is important if we're to use it in a container that only have two stages life-cycles invocation. In this case, we can simply invoke start() and destroy() to match to our service's life-cycles.
Some frameworks might go even further and create separate interfaces for each stage of the life-cycles, such as InitableService or StartableServiceetc. But I think that would be too much in a typical app. In most of the cases, you want something simple, so I like it just one interface. User may choose to ignore methods they don't want, or simply use an adaptor class.
Before we end this section, I would throw in a silly Hello world service that can be used in our demo later.
package servicedemo;

public class HelloService extends AbstractService {
    public void initService() {
        logger.info(this + " inited.");
    }
    public void startService() {
        logger.info(this + " started.");
    }
    public void stopService() {
        logger.info(this + " stopped.");
    }
    public void destroyService() {
        logger.info(this + " destroyed.");
    }
}

Managing multiple POJO Services with a container

Now we have the basic of Service definition defined, your development team may start writing business logic code! Before long, you will have a library of your own services to re-use. To be able group and control these services into an effetive way, we want also provide a container to manage them. The idea is that we typically want to control and manage multiple services with a container as a group in a higher level. Here is a simple implementation for you to get started:
package servicedemo;

import java.util.*;
public class ServiceContainer extends AbstractService {
    private List<Service> services = new ArrayList<Service>();

    public void setServices(List<Service> services) {
        this.services = services;
    }
    public void addService(Service service) {
        this.services.add(service);
    }

    public void initService() {
        logger.debug("Initializing " + this + " with " + services.size() + " services.");
        for (Service service : services) {
            logger.debug("Initializing " + service);
            service.init();
        }
        logger.info(this + " inited.");
    }
    public void startService() {
            logger.debug("Starting " + this + " with " + services.size() + " services.");
            for (Service service : services) {
                logger.debug("Starting " + service);
                service.start();
            }
            logger.info(this + " started.");
    }
    public void stopService() {
            int size = services.size();
            logger.debug("Stopping " + this + " with " + size + " services in reverse order.");
            for (int i = size - 1; i >= 0; i--) {
                Service service = services.get(i);
                logger.debug("Stopping " + service);
                service.stop();
            }
            logger.info(this + " stopped.");
    }
    public void destroyService() {
            int size = services.size();
            logger.debug("Destroying " + this + " with " + size + " services in reverse order.");
            for (int i = size - 1; i >= 0; i--) {
                Service service = services.get(i);
                logger.debug("Destroying " + service);
                service.destroy();
            }
            logger.info(this + " destroyed.");
    }
}
From above code, you will notice few important things:
  1. We extends the AbstractService, so a container is a service itself.
  2. We would invoke all service's life-cycles before moving to next. No services will start unless all others are inited.
  3. We should stop and destroy services in reverse order for most general use cases.
The above container implementation is simple and run in synchronized fashion. This mean, you start container, then all services will start in order you added them. Stop should be same but in reverse order.
I also hope you would able to see that there is plenty of room for you to improve this container as well. For example, you may add thread pool to control the execution of the services in asynchronized fashion.

Running POJO Services

RUNNING SERVICES WITH A SIMPLE RUNNER PROGRAM.

In the simplest form, we can run our POJO services on our own without any fancy server or frameworks. Java programs start its life from a static main method, so we surely can invoke init and start of our services in there. But we also need to address the stop and destroy life-cycles when user shuts down the program (usually by hitting CTRL+C.) For this, the Java has the java.lang.Runtime#addShutdownHook() facility. You can create a simple stand-alone server to bootstrap Service like this:
package servicedemo;

import org.slf4j.*;
public class ServiceRunner {
    private static Logger logger = LoggerFactory.getLogger(ServiceRunner.class);

    public static void main(String[] args) {
        ServiceRunner main = new ServiceRunner();
        main.run(args);
    }

    public void run(String[] args) {
        if (args.length < 1)
            throw new RuntimeException("Missing service class name as argument.");

        String serviceClassName = args[0];
        try {
            logger.debug("Creating " + serviceClassName);
            Class<?> serviceClass = Class.forName(serviceClassName);
            if (!Service.class.isAssignableFrom(serviceClass)) {
                throw new RuntimeException("Service class " + serviceClassName + " did not implements " + Service.class.getName());
            }
            Object serviceObject = serviceClass.newInstance();
            Service service = (Service)serviceObject;

            registerShutdownHook(service);

            logger.debug("Starting service " + service);
            service.init();
            service.start();
            logger.info(service + " started.");

            synchronized(this) {
                this.wait();
            }
        } catch (Exception e) {
            throw new RuntimeException("Failed to create and run " + serviceClassName, e);
        }
    }

    private void registerShutdownHook(final Service service) {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                logger.debug("Stopping service " + service);
                service.stop();
                service.destroy();
                logger.info(service + " stopped.");
            }
        });
    }
}
With abover runner, you should able to run it with this command:
$ java demo.ServiceRunner servicedemo.HelloService
Look carefully, and you'll see that you have many options to run multiple services with above runner. Let me highlight couple:
  1. Improve above runner directly and make all args for each new service class name, instead of just first element.
  2. Or write a MultiLoaderService that will load multiple services you want. You may control argument passing using System Properties.
Can you think of other ways to improve this runner?

RUNNING SERVICES WITH SPRING

The Spring framework is an IoC container, and it's well known to be easy to work POJO, and Spring lets you wire your application together. This would be a perfect fit to use in our POJO services. However, with all the features Spring brings, it missed a easy to use, out of box main program to bootstrap spring config xml context files. But with what we built so far, this is actually an easy thing to do. Let's write one of our POJO Service to bootstrap a spring context file.
package servicedemo;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class SpringService extends AbstractService {
    private ConfigurableApplicationContext springContext;

    public void startService() {
        String springConfig = System.getProperty("springContext", "spring.xml);
        springContext = new FileSystemXmlApplicationContext(springConfig);
        logger.info(this + " started.");
    }
    public void stopService() {
        springContext.close();
        logger.info(this + " stopped.");
    }
}
With that simple SpringService you can run and load any spring xml file. For example try this:
$ java -DspringContext=config/service-demo-spring.xml demo.ServiceRunner servicedemo.SpringService
Inside the config/service-demo-spring.xml file, you can easily create our container that hosts one or more service in Spring beans.
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="helloService" class="servicedemo.HelloService">
    </bean>

    <bean id="serviceContainer" class="servicedemo.ServiceContainer" init-method="start" destroy-method="destroy">
        <property name="services">
            <list>
                <ref bean="helloService"/>
            </list>
        </property>
    </bean>

</beans>
Notice that I only need to setup init-method and destroy-method once on the serviceContainer bean. You can then add one or more other service such as the helloService as much as you want. They will all be started, managed, and then shutdown when you close the Spring context.
Note that Spring context container did not explicitly have the same life-cycles as our services. The Spring context will automatically instanciate all your dependency beans, and then invoke all beans who's init-method is set. All that is done inside the constructor of FileSystemXmlApplicationContext. No explicit init method is called from user. However at the end, during stop of the service, Spring provide the springContext#close() to clean things up. Again, they do not differentiate stop from destroy. Because of this, we must merge our init and start into Spring's init state, and then merge stop anddestroy into Spring's close state. Recall our AbstractService#destory will auto invoke stop if it hasn't already done so. So this is trick that we need to understand in order to use Spring effectively.

RUNNING SERVICES WITH JEE APP SERVER

In a corporate env, we usually do not have the freedom to run what we want as a stand-alone program. Instead they usually have some infrustructure and stricter standard technology stack in place already, such as using a JEE application server. In these situation, the most portable way to run POJO services is in a warweb application. In a Servlet web application, you can write a class that implements javax.servlet.ServletContextListener and this will provide you the life-cycles hook via contextInitialized and contextDestroyed. In there, you can instanciate your ServiceContainer object and call start anddestroy methods accordingly.
Here is an example that you can explore:
package servicedemo;
import java.util.*;
import javax.servlet.*;
public class ServiceContainerListener implements ServletContextListener {
    private static Logger logger = LoggerFactory.getLogger(ServiceContainerListener.class);
    private ServiceContainer serviceContainer;

    public void contextInitialized(ServletContextEvent sce) {
        serviceContainer = new ServiceContainer();
        List<Service> services = createServices();
        serviceContainer.setServices(services);
        serviceContainer.start();
        logger.info(serviceContainer + " started in web application.");
    }

    public void contextDestroyed(ServletContextEvent sce) {
        serviceContainer.destroy();
        logger.info(serviceContainer + " destroyed in web application.");
    }

    private List<Service> createServices() {
        List<Service> result = new ArrayList<Service>();
        // populate services here.
        return result;
    }
}
You may configure above in the WEB-INF/web.xml like this:
    <listener>
        <listener-class>servicedemo.ServiceContainerListener</listener-class>
    </listener>

</web-app>
The demo provided a placeholder that you must add your services in code. But you can easily make that configurable using the web.xml for context parameters.
If you were to use Spring inside a Servlet container, you may directly use their org.springframework.web.context.ContextLoaderListener class that does pretty much same as above, except they allow you to specify their xml configuration file using the contextConfigLocation context parameter. That's how a typical Spring MVC based application is configure. Once you have this setup, you can experiment our POJO service just as the Spring xml sample given above to test things out. You should see our service in action by your logger output.
PS: Actually what we described here are simply related to Servlet web application, and not JEE specific. So you can use Tomcat server just fine as well.

The importance of Service's life-cycles and it's real world usage

All the information I presented here are not novelty, nor a killer design pattern. In fact they have been used in many popular open source projects. However, in my past experience at work, folks always manage to make these extremely complicated, and worse case is that they completely disregard the importance of life-cycles when writing services. It's true that not everything you going to write needs to be fitted into a service, but if you find the need, please do pay attention to them, and take good care that they do invoked properly. The last thing you want is to exit JVM without clean up in services that you allocated precious resources for. These would become more disastrous if you allow your application to be dynamically reloaded during deployment without exiting JVM, in which will lead to system resources leakage.
The above Service practice has been put into use in the TimeMachine project. In fact, if you look at thetimemachine.scheduler.service.SchedulerEngine, it would just be a container of many services running together. And that's how user can extend the scheduler functionalities as well, by writing a Service. You can load these services dynamically by a simple properties file.

Source: http://saltnlight5.blogspot.gr/2012/09/how-to-write-better-pojo-services.html

Sunday, August 26, 2012

How to detect user location using Google Maps, Geolocation API and HTML5

Source: http://yourravi.com/detect-user-location-using-google-maps-geolocation-api-html5/




Seen Google Maps? Amazing isn’t it? Wanna use it to track your friend’s or girlfriend’s location. If yes, then just sit back and read our monthly superpost wherein we’ll explain how Google Maps can be used in conjunction with Geolocation API and HTML 5 to detect anyone’s location on Map.
Not only this, you can also track if the subject is changing location – whether he/she is moving and where he/she is heading.
In the tutorial, we’ll do simple stuff – just mark user location and show it on map. Here is the demo of what we’re building. [Click 'Allow' when asked for permission]
For easy grasp, we’ll break the code in pieces and discuss every bit in detail.
Let’s start,

First, some Terminology

1. Geolocation API: The API provides additional properties to navigator object that helps in detecting user’s (browser, in fact) latitude and longitude positions. These values are detected and transferred to Google Maps as parameter, which shows the location accordingly on map. All modern browsers those support HTML5 – IE9+, Firefox 3.5+, Safari5+, Chrome5+, Opera10.6+, iPhone3+, Android2+ support Geolocation API. [More on Geolocation API]
2. Google Maps API: This API define functions for easy interaction with Google Maps. API functions take coordinate values (latitude and longitude) of user location and shows it on map as output. [More on Google Maps API]
3. HTML5: It’s as advanced version of HTML with added features. All modern browsers supporting HTML 5 provides inbuilt support to Geolocation API as well. [More on HTML5]

How Maps would appear in output

In the HTML code, we do the following things,
  1. Specify <!DOCTYPE html> according to HTML5 standards,
  2. Load Google Maps JavaScript API <script src="http://maps.google.com/maps/api/js?sensor=false"></script> to use its functions for interaction with Google Maps.
  3. Define two divs – #location_div where location name would show up (like Delhi, India), and #map_div is where the output map would appear.
  4. Height and width of all elements is set to 100% as we want to show the map over the entire webpage.
Program execution starts when the JavaScript function location_info() is called on onLoad() event in the <body></body> tag. Here is the code,
<!DOCTYPE html style='width:100%;height:100%;'>

<head>
 <title>Tracking location using Google Maps API, Geolocation API in HTML5 supporting browsers
 </title>

<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
</head>

<body onload='location_info()'>
 <div id='location_div' style='background:black;'></div>
 <div id='map_div' style='width:100%;height:100%;'></div>
</body>
Next is the big JavaScript code that does all the work. Here we first get the coordinates of user, then send them to Google Maps and display the returned Map with user location marked on it.

Get coordinates, send to Google Maps API and show location on Map

1. location_info function is called on page load JS event. This function calls the getCurrentPostion() method of Geolocation API, which must have 2 callback functions – one that’ll be called when program runs normally (get_coords) and other one when there is an error in execution (if_error).
function location_info()
{
 navigator.geolocation.getCurrentPosition(get_coords, if_error);
}
Additional Info: Besides, there are 3 optional arguments – enableHighAccuracy, timeout and maximumAge.
  1. enableHighAccuracy is a Boolean argument with true/false value. By default, its false. When its turned true, results are more accurate but there could be delay in fetching result.
  2. timeout is the maximum time program can wait for results to appear. It’s counted after user consents to detect his location.
  3. maximumAge is time for which a location information for user should remain in browser cache. Suppose, maximumAge is set to 12000 ie 2 minutes then location retrieved at 10:00am till 10:02am would be the same. It’ll alter after the maximumAge is elapsed.
Here is how the function would appear with both callback functions and optional arguments.
function location_info()
{
 navigator.geolocation.getCurrentPosition(get_coords, if_error, {enableHighAccuracy:true, timeout:10000, maximumAge:75000});
}
2. In the next function, we collect the longitude and latitude coordinates for a user and send to google.maps.LatLng() function as parameter.
We also define zoom level, type of map in mapsOptions array. This array is then passed to google.maps.Map() function that creates an object of Map class.
An object of Marker class – marker is created to mark the location in red colored sticker.
reverse_geocode() function is called at the end that returns address name to users longitude and latitude coordinate.
function get_coords(position)
{
 var latitude=position.coords.latitude;
 var longitude=position.coords.longitude;

var mapsOptions={
 zoom: 12,
 center: new google.maps.LatLng(latitude, longitude),
 mapTypeId: google.maps.MapTypeId.ROADMAP
 }

var map=new google.maps.Map(document.getElementById("map_div"), mapsOptions);

var marker = new google.maps.Marker({
 position: pos,
 map: map,
 title: "User location"
 });

reverse_geocode();
}
3. Location name for user coordinates
Using geocode() method of Geocoder class, we can map the near-exact address for a pair of longitude and latitude coordinates. Here, we already have the coordinates and hence we can get the location as well.
Look at the following code that displays address inside the #location_div,
function reverse_geocode()
{
 geocoder.geocode({'latLng': pos}, function(results, status){
 if (status == google.maps.GeocoderStatus.OK)
 {
 if (results[1])
 {
 document.getElementById('location_div').innerHTML="<p style='margin:0px;padding:5px;color:white;font-weight:bold;font-size:12px;color:#ededed;font-family:Tahoma;'> Now you're at <span
 style='color:white;'>"+results[1].formatted_address+"</span></p>";
 }
 }
 else
 {
 alert("Geocoder failed due to: " + status);
 }
 });
}

Error handling

There may occur some problems while detecting user’s geolocation. In such cases, the second callback function if_error() is called instead of get_coords().
Below are the error values that might pop-in,
  • 1 is returned when user denies geolocation tracking.
  • 2 is when the google maps doesn’t return geolocation value either because coordinates are wrong or an app exhausted the given number of free requests, or any other reason.
  • 3 is when the app/program is kept waiting for time longer than what specified in the getCurrentPosition() function.
function if_error(err)
{
 if(err.code==1){alert('User Denied');}
 if(err.code==2){alert('Position unavailable');}
 if(err.code==3){alert('Timeout');}
}
On executing the code, this is how it’ll run. First user would see a opt-in in browser that’ll ask for user consent.
permission for detecting user location using geolocation API in chrome browser
If user allows, coordinates are sent to Google maps and result is retrieved and shown. If user denies, error 1 pops-in an alert with message ‘User Denied’.
The location sharing system is highly secure and programs can only read location if user grants permission. The entire system is fool-proof and location can’t be read by any other means.
(Post under updation…)

Let your friends find this article, just share it!