Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, September 8, 2018

PGP Encryption using Apache Camel

PGP (Pretty Good Privacy) Encryption is a widely used encryption mechanism which can be used to provide public key encryption.
There are many PGP encryption implementations using Java language which are quite complex. But Apache Camel Crypto component facilitates a very simple and convenient way of implementing PGP encryption.
Following code encrypts a given message using Apache Camel. org.apache.camel:camel-crypto is the only one library i have used here.
Most of the example in the internet shows the way which a file is used as the input for the encryption. But here i will show you how to directly encrypt a string (text message).
I'm using the public key in the public.gpg file to encrypt the message. The code is really straight foreword. Complete working project is available on GitHub and the URL is available at the bottom. All required key files are available there. You can clone the project and execute the program.

Following is the GitHub URL of complete project of this tutorial.
https://github.com/lakjcomspace/PGPEncryption

Tuesday, November 24, 2015

OrientDB - Using plocal Embedded Storage from Two Processes

OrientDB has this nice feature of creating and using database in embedded mode. We can programatically create and accesses OrientDB database for CRUD operations without the server. We can create a plocal storage (Paginated Local Storage) with this. plocal is a disk based storage which works with data using page model. This storage is very fast because of we can assess the database directly with local mode.

But, plocal storage has a limitation; it can only be accessed by one process at a time. When a process accesses the database, it locks the database so that no other program or process can access that. This lock will not be released until the program terminates. It avoids updating the database from several processes.
If you are going to accesses the plocal db while another process has acquired the lock, you will have the following error message.

com.orientechnologies.orient.core.exception.OStorageException: Cannot open local storage 'C:/MyOrientDb/dbs/myPlocal' with mode=rw
at com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage.open(OAbstractPaginatedStorage.java:217)
at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.open(ODatabaseDocumentTx.java:243)
at com.lakj.comspace.orientdb.PlocalTest.readDb(PlocalTest.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)...
Caused by: com.orientechnologies.orient.core.exception.OSerializationException: Cannot load database's configuration. The database seems to be corrupted.
at com.orientechnologies.orient.core.storage.impl.local.OStorageConfigurationSegment.load(OStorageConfigurationSegment.java:86)
at com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage.open(OAbstractPaginatedStorage.java:164)
... 28 more
Caused by: java.io.IOException: The process cannot access the file because another process has locked a portion of the file
at java.io.RandomAccessFile.read0(Native Method)
at java.io.RandomAccessFile.read(Unknown Source)
at com.orientechnologies.orient.core.storage.fs.OAbstractFile.openChannel(OAbstractFile.java:641)
at com.orientechnologies.orient.core.storage.fs.OAbstractFile.open(OAbstractFile.java:144)
at com.orientechnologies.orient.core.storage.impl.local.OSingleFileSegment.open(OSingleFileSegment.java:57)
at com.orientechnologies.orient.core.storage.impl.local.OStorageConfigurationSegment.load(OStorageConfigurationSegment.java:65)
... 29 more

 I found a solution to this problem!

When you want to share a plocal database among two (or more) processes, you can follow the following procedure.

Let's take a sample scenario. Two processes called Process-A and Process-B are going to access the same plocal storage named 'myPlocal'. Process-A reads the database. Then Process-B update the database and terminates. Then again Process-A reads the database.

Step 1:
Create the db connection from Process-A read the relevant data.
When the Process-A creates the db connection, it locks the db so that any other process cannot read or update the db.
ODatabaseDocumentTx dbProsA = new ODatabaseDocumentTx("plocal:/db/myPlocal");
dbProsA .open("admin", "admin");
Step 2:
Freeze the db from the Process-A.

Freeze method is used to create 'live' database backups. It flushes all cached content to the disk storage and allows to perform only read commands for current process. Here when the Process-A calls freeze method, no any thread from Process-A can write on db. But any thread from Process-A can read from db. More importantly, another process can acquire the db lock and update it. Here the Process-B will do it.
dbProsA .freeze();
Step 3:
Create the db connection from Process-B and read or update data. Then the Process-B terminates.
Because of Process-A has freezed it's connection Process-B can create a new connection and update the db. Here the Process-B should terminate and release the db so that the Process-A can get it back.
ODatabaseDocumentTx dbProsB = new ODatabaseDocumentTx("plocal:/db/myPlocal");
dbProsB.open("admin", "admin");
// Do whatever you want here 
dbProsB.close();
Step 4:
Process-A release the db.
If you freeze the db connection, you should call release method to enable it for write operations.
dbProsA .release();
Step 5:
Process-A restarts the db engine.

This is the most important step. Though you have release the db connection from Process-A, it won't be able to read the updates which has been done by Process-B. You may still read the data which you had at the point you freeze it.
To read the updated values from the Process-B you should re-start the db engine from Process-A. It's just two method calls; shutdown() and start().
OOrient instance = Orient.instance();
instance.shutdown();
instance.startup();
You are done..!
Now you have shared your plocal db between Process-A and Process-B.

Sunday, January 25, 2015

IntelliJ Plugin Development Tutorial - Project View Popup Menu Action Item

This simple tutorial shows you how to create an Action Item in the Project View Popup Menu.
When  you right click on the project view you will have this action item in the popup menu. You can extend or use this to execute any action.

 Following are the two files you should work with.

Following are the GitHub URL's for complete plugin project. You can clone the project or downolad the project as a zip file. To run the project open IntelliJ IDEA and import the project.

GitHub repository URL - https://github.com/lakjcomspace/IntelliJProjectViewMenuAction
GitHub HTTPS clone URL - https://github.com/lakjcomspace/IntelliJProjectViewMenuAction.git
Download Project as a Zip - https://github.com/lakjcomspace/IntelliJProjectViewMenuAction/archive/master.zip



Monday, May 5, 2014

Simple Android Client-Server Application

This is an android application which sends a text message to a server. When you press the send button, the application sends the message in the text field to the server program. Here the program been written so that the client runs on the Android emulator and the server runs on the local host. IP address 10.0.2.2  can be used to connect to the local host with Android. Server program is continuously listening to the port 4444. When an incoming message arrived, server read it and show it on the standard output.
This tutorial is based on Android 4.4 (API Level 19 - KITKAT) version.



Following are the source codes of main components.

Android Text Client Application




Java Sever Program



Following are the both Android client and Java server complete projects. You can clone or download them as zips from Git-Hub.

Git-Hub HTTPS clone URL: https://github.com/lakjcomspace/AndroidTextClientServer.git
Git-Hub Repository URL: https://github.com/lakjcomspace/AndroidTextClientServer
Download as a Zip: https://github.com/lakjcomspace/AndroidTextClientServer/archive/master.zip
You can run the projects by importing them to the IDE. Or just create you own project and copy and paste above source files.

Sunday, August 11, 2013

Erase Some Characters from Each Line of a Text File - Java Program

Following Java program erase user given fixed number of characters from each line of a text file. Here the number of characters to be erased is 3. For the convenience input and output files are given as two separate file.  Output file may contain the result you want.



Tuesday, May 22, 2012

Brightness Changing Algorithm - Image Processing Basic

There are many libraries which are used to Image processing applications. But the algorithms behind these libraries use basic mathematical operation. I hope to bring you series of blog posts which describe the fundamental image processing algorithms implemented in mathematical operation without using any library. Following java class change the brightness of an image.
We should input our image to the method changeBrightness as a BufferedImage. increasingFactor is an integer which determine the brightness level of the image. Positive values will increase the brightness while negative values decrees. Range of the increasingFactor should be between -255 to 255.

Original Image
Brightness Increased
Brightness Decreased

Friday, March 30, 2012

Android Application to Send a File to Remote Server

This simple Android application sends a file to a remote server. In this example code server runs on local host. When the Send button is clicked the file is sent to the server.

Android Client Application
Server Application

Thursday, March 29, 2012

Simple Client-Server Application for Android


Note: Here is the updated tutorial of the following tutorial for the latest Android version:
http://lakjeewa.blogspot.com/2014/05/simple-android-client-server-application.html 

Following tutorial is based on Android 2.3 (API Level 10). It is not compatible with later versions since API Level 11 introduced AsyncTask implementation for network communication. Refer the above link instead for working solution. 

This application is a simple client-server application which has a Android mobile client and a Java server which is run on a machine. In this example, client is run on the Android emulator and the server is run on the local host. In Android 10.0.2.2 is the IP address for local host. This application allow to type a text message on a text field and when the Send button is press the message is sent to the server. Server continuously listen to the port. When there is a incoming message server read it and show it on the standard output.



Client Side Application



Server Side Application

Friday, February 10, 2012

Roman Number Converter - Java Program

This is a simple Java program to convert Roman numbers to Hindu-Arabic numbers. This Java method "convertRomToInt()" gets the Roman number as a string and converts it in to corresponding Integer value.
Even though this program seems simple, the algorithm is bit complex. When we convert a Roman number to Hindu-Arabic number manually, we do it in the following manner.
MCMXLIV = 1000 + (1000 − 100) + (50 − 10) + (5 − 1) = 1944
Value of each number is added together. When smaller values precede larger values, the smaller values are subtracted from the larger values, and the result is added to the total.
This algorithm use above technique to the conversion. "for" loop gets each character of the roman number string from the beginning and gets its integer value from the "getInt()"  method. At each iteration character is compared with the next character value. If the next character is the same, the value is added to mediator to add in the next iteration. If it is large, the value is added to mediator to subtract in the next iteration.