Converting json String to a Map, without Dependencies

Starting with JDK 8u60+ the built-in Nashorn engine is capable of converting JSON content into java.util.Map.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class JSONParsingTest {

    private ScriptEngine engine;

    @Before
    public void initEngine() {
        ScriptEngineManager sem = new ScriptEngineManager();
        this.engine = sem.getEngineByName("javascript");
    }

    @Test
    public void parseJson() throws IOException, ScriptException {
        String json = new String(Files.readAllBytes(/*path*/);
        String script = "Java.asJSONCompatible(" + json + ")";
        Object result = this.engine.eval(script);
        assertThat(result, instanceOf(Map.class));
        Map contents = (Map) result;
        contents.forEach((t, u) -> {
        //key-value pairs operations
        });
    }
}


Reffer Nashorn extensions documentation for more use cases.

Technology Stack at The Enterprise Level

The choice of technology stack at the enterprise level, especially when extending or expanding the offerings has lots of considerations:
* What does my tech ops know how to install, deploy, support? Introducing new technology is expensive.

* What does my existing staff know? Introducing new technology is expensive.

* What technology stack can I get indemnified libraries for? Introducing new technology is risky?

* What is the existing technology stack I need to interface with? Cross architecture connectivity can be expensive and suffer performance issues.

* What technology stack is used for the supported open-source solutions I might be interested in deploying? Bleeding edge can be expensive.

* What technology stack is used for the proprietary solutions I might be interested in deploying? Asking/paying for porting to my preferred stack is expensive.

* What does everyone else use? It’s politically safer to be a technology follower, especially with the high rate of software project failure.

* What makes programmers most efficient and effective? They are a major cost factor.

There are other considerations
* Technical fit – not likely to do browser plug-ins in COBOL.

* Maturity of the stack – what’s proven, secure, performing,

* The “–abilities”, support, scale, secure, maintain, development, port, upgrade, use, interopera, etc.?

* What does the CTO believe?

* What does the “though leaders” in the development organization believe?

* What does the company you are about to acquire use?

* What does the company about to acquire you use?

* What does your parent (or child) company use?

I’m sure I’m forgetting some, and clearly these all don’t carry the same weight. And, depending on the scope of the project, there’s different weights as well.

You get the picture. The choice is seldom based simply on the technical purity of the programming language. While that’s fun to debate, from a purely academic point of view, the real world is much messier.

Why Enterprises uses java? because enterprise apps are not about UI widgets only. Enterprise apps are running on multiple devices, supporting millions of concurrent users, with 99.99% of uptime.

So to achieve this its not about choosing the language only but the whole solution. Which includes Security, Persistence api, Transaction management, Clustering, Messaging, Webservices, ESB, Connectors for heterogeneous applications, Web support etc. You can see the reason why enterprise uses Java as its major programing language as JEE answers for most of these issues. Other than that there are many opensource solutions available and there is a big community behind it.

Enterprises are also using Javascript as a key component for their front end development regardless of what backend used. Specially for AJAX and SPA.

And if by Javascript you meant node.js, than we probably talking about the microservices and may be MEAN stack. which is a different application architecture. In that case you have opportunity to write Javascript both on server and client side, but even in that case there are still some Java components being used for integration.

Exposing REST Services Using Swagger

Swagger is a simple yet powerful representation of your RESTful API. With the largest ecosystem of API tooling on the planet, thousands of developers are supporting Swagger in almost every modern programming language and deployment environment. With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability.

First thing you need to do when you use Swagger is use @Api annotation for describe endpoint.

Selection_061

Next thing need to add is @ApiOperation annotation. This will create a UI for the API.

Selection_062

Above annotation will create a following UI for you, You are building the documentation UI while you are creating the API. You don’t have to create a separate documentation.

Selection_063

Then you can add errors for rest api usage.

Selection_064

If some one wants to add specific value and test the api, you can implement it in documentation by @ApiParam annotation. All the details of the parameter can be describe in this doc too.

Selection_065

This is the full api code wrap with Swagger annotation.

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.model.Account;
import com.example.service.AccountService;
import com.force.api.ApiException;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiError;
import com.wordnik.swagger.annotations.ApiErrors;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

@Controller
@RequestMapping(value = "/api/v1/account")
@Api(value = "Account operations", listingClass = "AccountController", basePath = "/api/v1/account", description = "All operations for accounts")
public class AccountController {
	
	@Autowired
	AccountService accountService;
	
	@ApiOperation(value = "Get all account (max:200)", notes = "Get all existing account names (max:200)", httpMethod = "GET", responseClass = "Account", multiValueResponse = true)
	@ApiError(code = 500, reason = "Process error")
	@RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/json")
	public @ResponseBody List<Account> showAllAccounts() throws ApiException  {
	    return accountService.listAccounts();
	}
	
	@ApiOperation(value = "Get a existing account by Id", notes = "Get account name by specifying a Salesforce account id", httpMethod = "GET", responseClass = "Account", multiValueResponse = true)
	@ApiErrors(value = { @ApiError(code = 400, reason = "Invalid Id supplied"), @ApiError(code = 404, reason = "Account not found") })
	@RequestMapping(value = "/find/{accountId}", method = RequestMethod.GET, produces = "application/json")
	public @ResponseBody Account findAccountById(@ApiParam(internalDescription = "java.lang.string", name = "accountId", required = true, value = "string") @PathVariable String accountId) throws ApiException {
		return accountService.findAccountById(accountId);
    }
	
	@ApiOperation(value = "Delete a account giving an Id", notes = "Delete a account by specifying a Salesforce account id", httpMethod = "DELETE")
    @ApiError(code = 500, reason = "Process error")
    @RequestMapping(value = "/delete/{accountId}", method = RequestMethod.DELETE, produces = "application/json")
	public @ResponseBody String deleteAccount(@ApiParam(internalDescription = "java.lang.string", name = "accountId", required = true, value = "string") @PathVariable String accountId) throws ApiException {
		return accountService.deleteAccount(accountId);
    }
	
	@ApiOperation(value = "Create a new account", notes = "Creates a new Salesforce account by specifying a account name", httpMethod = "POST")
    @ApiError(code = 500, reason = "Process error")
    @RequestMapping(value = "/new", method = RequestMethod.POST, produces = "application/json")
	public @ResponseBody String createAccountFromParamName(@ApiParam(internalDescription = "java.lang.string", name = "name", required = true, value = "string") @RequestParam(required = true) String name) throws ApiException{	
		return accountService.createAccount(name);
    }
	
	@ApiOperation(value = "Update an exsiting account by Id", notes = "Update a existing account by specifying a Salesforce id", httpMethod = "POST")
	@RequestMapping(value = "/update/{accountId}", method = RequestMethod.POST, produces = "application/json")
	public @ResponseBody String updateAccount(@PathVariable(value="accountId") String accountId, @ApiParam(internalDescription = "java.lang.string", name = "name", required = true, value = "string") @RequestParam(required = true) String name) throws ApiException {
		Account updateAccount = new Account();
		updateAccount.setName(name);
		return accountService.updateAccount(accountId, updateAccount);
	}
}

This will create the following documentation UI.
Selection_066Lets try to create a new account and test the API. Click ‘try it out’ after providing the name parameter. Selection_067

It will shows the response body,code and any headers for the invoking above rest operation.
Selection_069I guess this would help you to get rough idea about Swager tool for API documentation. You can find complete project here

Obscure trends in open source

Annual Best of Open Source Software Awards, affectionately known as the Bossies , was held few days back. As you browse through the Bossies, one thing that will strike you is how essential so much of open source software has become to modern computing, particularly in application development and IT infrastructure. According to Bossies awards these are the trends  relatively obscure to the world at large.

1. Open source is ground zero for technology development. Once software vendors would open source software that, to put it kindly, wasn’t worth monetizing anymore. Now open source has become the preferred way of germinating hot new technology, particularly for startups. Docker and Hadoop — and in particular their exploding ecosystems — are the most obvious examples of this, not to mention the parade of NoSQL and NewSQL databases.

2. The cloud is eating open source applications. Browse through the “best open source applications” section of the Bossies, and you’ll see that many have a SaaS or hosted option. Makes sense — even Microsoft Office 365 is a semi-cloud offering and its chief rival is clearly Google Apps. These days, many IT departments would like to avoid installing and maintaining applications locally when possible.

3. Big Internet companies are huge open source contributors. You probably know that Google came up with MapReduce and Yahoo cooked up Hadoop. Maybe you also know that Google developed the most popular JavaScript framework, AngularJS, and contributed cgroups to the Linux kernel, which eventually became Docker — today’s open source darling. But did you know that eBay, Facebook, LinkedIn, Netflix, and Twitter have each originated dozens of open source projects? Facebook, for example, is responsible for both Cassandra, one of the leading NoSQL databases, and React, a wildly popular JavaScript library.

4. GitHub is the center of the universe. The world’s leading code repository and versioning system, offered as a cloud service, now hosts 27 million projects. Sure, you’ll find most of the high-profile open source projects here — and/or at Apache. But the point is that GitHub has changed software development forever by answering a simple question: Why code it yourself if someone else has already done it and is willing to share under a liberal open source license? Odds are, you can find something close to what you need on GitHub.

5. Security has become a major sore point. Last year was a tough one for open source security. We all know the Heartbleed saga: A flaw in OpenSSL stood unaddressed for two years, and once discovered threw admins everywhere into a panic. Six months later, we were confronted with the nasty Shellshock bug, which had lurked in the open source Bash project since 1989. The rising importance and ubiquity of open source solutions make them big fat targets, so the industry has been forced to collaboratively fund projects like OpenSSL that were woefully underresourced.

Cray® XC™ Series ‘Edison’ Supercomputer Helps Model 3-D Map of Adolescent Universe

Using extremely faint light from galaxies 10.8 billion light years away, scientists have created one of the most complete, three-dimensional maps of a slice of the adolescent universe — just 3 billion years after the Big Bang. The map shows a web of hydrogen gas that varies from low to high density at a time when the universe was made of a fraction of the dark matter we see today. It was created in part using the Cray® XC30™ supercomputer “Edison” at the National Energy Research Scientific Computing Center (NERSC) by a team that included researchers from Berkeley Lab’s Computational Cosmology Center (C3).

Cray XC30 ‘Edison’ Overview

  • • Cabinets: 30
  • • Peak Performance: 2.57 PF
  • • System Memory: 357 TB
  • • Compute Nodes: 5,576 12-core Intel® Xeon® “Ivy Bridge” processors (24 cores per node)
  • • Compute Cores:133,824
  • • Interconnect: Aries
  • • Storage: 7.56 PB
  • • I/O Bandwidth: 163 GB

Cray and NERSC share a long history, starting with a Cray-1 in 1978. Its current Cray systems are the Cray® XE6™ “Hopper” system and Cray® XC30™ “Edison” supercomputer. In 2016, Cray will deliver “Cori” — a nextgeneration Cray® XC™ system that is expected to deliver 10 times the sustained computing capability of the Hopper supercomputer.