Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Sunday, March 16, 2014

JavaSE: How to SET/GET your own Files and Directory Attributes

In the previous article "Determining Views Supported by a Particular File System" and see how to ask the file system store, and see supportability of a particular file attribute view.

We are going to explore in a nutshell, one of the most advanced and important files attribute views which is User-Defined File Attributes View.

Particularly, I am using this feature a lot in my exchanged files during integration between my systems, to hide my files meta-data and security related information from users and contents of the file. So the file content will be only regarding the content of the file only, no more irrelevant meta-data.

Therefore If you find that there are not enough built-in attributes for your needs or if you have some unique meta-data (meaningful to the file system) that you want to associate with a file, you can define your own attributes.

NIO.2 offers the user-defined file attributes view, extended attributes through the UserDefinedFileAttributeView interface. This facility allows you to associate to a file any attribute that you consider to be useful for your use cases.

Here you should know how to:

  1. Check User-Defined Attributes Supportability
  2. Operations on User-Defined Attributes as the following:
    1. Define a User Attribute.
    2. List User-Defined Attribute Names and Value Sizes.
    3. Get the Value of a User-Defined Attribute.
    4. Delete a File’s User-Defined Attribute.

Here is the class that has operations defined previously, also you need to use JDK 7+:


References


Thursday, March 13, 2014

JavaSE 7,8: Determining Views Supported by a Particular File System

If you have questions about a file or a directory, such as whether it is hidden, whether it is a directory, what its size is, and who owns it, you can get answers to those questions (and many others) from the metadata, which is data about other data.

NIO.2 associates the notion of metadata with attributes and provides access to them through the java.nio.file.attribute package. Since different file systems have different notions about which attributes should be tracked, NIO.2 groups the attributes into views, each of which maps to a particular file system implementation.

Generally, views provide the attributes in bulk through a common method, readAttributes(). In addition, you can extract and set a single attribute with the getAttribute() and setAttribute() methods, respectively, which are available in the java.nio.file.Files class. Depending On the view, other methods are available for additional tasks.

Here i will explain how with NIO.2 you can manage more details about files’ metadata than ever before. Attributes are divided into categories, and now they cover POSIX systems as well.

Before you attempt to access a view's attributes, make sure that your file system supports the corresponding view.

NIO.2 comes with a set of six views (Basic, Dos, POSIX, FileOwner, ACL and UserDefinedFileAttributeView).
  • POSIX (Portable Operating System Interface for UNIX).
  • All file systems support the basic view, so you should get at least the basic name in your output.

The supported views are:

  1. BasicFileAttributeView
    This is a view of basic attributes that must be supported by all file system implementations. The attribute view name is basic.
  2. DosFileAttributeView
    This view provides the standard four supported attributes on file systems that support the DOS attributes. The attribute view name is dos.
  3. PosixFileAttributeView
    This view extends the basic attribute view with attributes supported on file systems that support the POSIX (Portable Operating System Interface for Unix) family of standards, such as Unix. The attribute view name is posix.
  4. FileOwnerAttributeView
    This view is supported by any file system implementation that supports the concept of a file owner. The attribute view name is owner.
  5. AclFileAttributeView
    This view supports reading or updating a file’s ACL. The NFSv4 ACL model is supported. The attribute view name is acl.
  6. UserDefinedFileAttributeView
    This view enables support of metadata that is user defined.

Here are code snipets that shows different operations

  • Get all supported file system views

  • Check if the file store support a specific view
    You can test a particular view on a file store by calling the FileStore.supportsFileAttributeView() method. You can pass the desired view as a String or as a class name.

  • Check if a file store in which a particular file resides supports a single view
    Moreover, you can check if a file store in which a particular file resides supports a single view.


That all, have fun, if you like it share it.

Refrences:

Wednesday, April 17, 2013

Mac: Some terminal and Unix/Mac shortcuts

Here I will give you some interesting shortcuts, that every one of us need when working on daily bases with unix or mac and their terminal:
  1. Up/Down arrows:
    Preview previews commands. It saves your time when you need the previously typed commands again and again for execution.

  2. control + a:
    Moves the cursor to start of the line. It is very useful when you got a long line about fifteen chars or more, and you need to move to the start of the line or close to the start of the line to do some changes, this shortcut will move you to the start very quickly instead of returning character by character.

  3. control + e:
    Moves the cursor to end of the line. So you can remember that by "a" for alphabit and "e" for the end.

  4. option + click line (Mac OS x and terminal only):
    Moves the cursor to the click point.

  5. tab (autocomplete):
    Try to complete the command or (file or folder name).

  6. tab + tab:
    It is useful when tab doesn't complete, pressing tab twice will show the list of available possible matches.

  7. command + ~:
    Cycle between terminal windows (terminal only), for GUI use command + tab.

  8. command + k:
    Will clear the screen and scroll back(terminal only).

  9. command + plus(+):
    Makes fonts bigger.

  10. command + minus(-):
    Makes fonts smaller.

  11. option + command + T:
    Open the characters map viewer.

  12. command + E:
    Find using the selected text.

  13. command + S:
    Export terminal text as.

  14. shift + command + S:
    Export selected text as.

  15. Triple-click the line
    Select a complete line of text.

  16. Double-click the word
    Select a word.

Use these shortcuts to save time while using Terminal.


Saturday, February 2, 2013

Barcode: Developing advanced Barcode applications using "BarcodeEngine" library.


Learn how to generate and read back any generated barcode using Barcode Engine 1.3 library.

Introduction

Developing Barcode API that decode and encode barcode images (or binary streams) from your application, integration with hardware or even your mobile is a demanding market requirement nowadays.
I will demonstrate my experience in tutorial like steps, to explain how to develop a barcode application to generate and consume back generated barcode to extract the encoded data, also decode any other generated picture that contains barcode, using my developed barcode library BarcodeEngine 1.3L (L for library).
It is useful to read the first article about the barcode (Barcode: Professional java barcode API (reader & writer)), to get the basic idea that will make it easy to understand this article.
Also the required software to install to be able to compile and run my BarcodeEngine 1.3L as it depends on two libraries the core.jar and javase.jar jars.

Software Pre-requisites

The following pieces of software are required to develop your barcode application:
  1. The core.jar, javase.jar jars.
  2. The BarcodeEngine 1.3L and could be downloaded from here.

Prepare the project

After getting the required software libraries as described in previous section. Now we are going to create your first project that will be used to run the barcode engine. I will use the Netbeans IDE (7.x) here, but indeed you are free to develop on your favorite IDE.
The following steps we are going to walk through will guides you to complete the barcode engine application:
  1. Open Netbeans and press (Ctrl + Shift + N) to open create project window, under categories list choose Java then in the other right hand list (Projects) choose Java Application then press next.

  2. In next window Name and Location write the following:


  3. Press Finish button and you should see something like this.


  4. Now we are going to attach the libs which we got earlier from the pre-requisites section, right click on the project and choose properties. In the categories choose libraries and on right hand side under compiler tab choose Add JAR/Folder and navigate to your libs to attach them then click open as the following snapshot.


  5. If all libs are added successfully, you should see something like this.


  6. Now our project is ready for developing barcode application.

Developing the application

Now after everything is setup successfully and ready. Let's explore the BarcodeEngine v1.3 library and do the coding to generate and read the barcodes, using proper configurations.
  1. Open already created class named "BarcodeApplication" which contains the main method.
  2. Inside the main method copy and paste the following code:
  3. Run the code and you should find the generated barcode images created in the specified folder as the following:


  4. In the output console you should see the following output:

How it works

Now after we have created our application, and see the created barcodes and reading back its contents, let's explore the application and see how it works.
First we have declared two files for two different barcode types, qrCodeFile and PDF417File in a specific folder which doesn't exists.
The second step is to create the encoding configuration (instance of EncodeConfig) which used by the barcode engine, encode method as hints for encoding barcode contents to the type we need to generate. The createDirectories(Boolean.TRUE) is responsible for forcing the engine to create the directories if it doesn't exists.
Note: that EncodeConfig and DecodeConfig configuration classes using builder pattern for creating the instances.
Then we have defined the contents to be encoded into QRCode as the following.
Then after configuration we asked the BarcodeEngine to encode the contentto qrCodeFile using encodeConfig encoding configurations. We did the same for the other type PDF417File. I do this to show up how to create the different types of barcodes.
Now it is the time to read back the barcode type's contents which we have generated before, using our BarcodeEngine API to decode and get back the results.
As we did before we need to configure the decoding configuration using a DecodeConfig instance, to be able to read the contents of any image containing barcode.
We pass the created decoding configuration along with the barcode file need to be decoded, to BarcodeEngine decode method, which return the result in form of map of DecodeResult enum as key and object as value.

We get the values from map as the following:


Note: All the BarcodeEngine API methods are static methods, and contain an Utils inner class that has many general useful methods.
Barcode engine also has other methods that extract the barcode from within an image, crop certain portion of the image, and rotate image. Consult the java documentation associated with API for more information.

Explain the API

Consult the java documentation associated with API for more information, about BarcodeEngine class, and its configuration classes (Encode and Decode configurations), alongside with the utility classes.

Finally

The final project source could be downloaded from here.

Friday, November 23, 2012

JDK7: Part 2- The power of java 7 NIO.2 (JSR 203) (Code Examples)

Get educated with the new file I/O mechanism introduced in the JDK 7 release.

In Part 1 I introduced the important concepts about new JSR 203 shipped and delivered with the latest release of Java development kit 7.

Here I am covering the important aspects involved in developing NIO.2-based applications by examples and case studies for the concepts introduced in part 1, this will spice up your Java 7 applications with the new I/O capabilities. You will learn to develop NIO.2 applications, beginning with simple but essential stuff and gradually moving on to complex features such as sockets and asynchronous channels.

In this second part I shipped it with fully coding examples for the following topics:
  1. Working with the new Path Class.
  2. Metadata File Attributes.
  3. Working with Symbolic and Hard Links.
  4. New API for Files and Directories.
  5. The FileVisitor Interface.
  6. Monitoring via Watch Service API.
  7. New powerful Random Access Files.
  8. Networking with the Sockets APIs.
  9. The Asynchronous Channel API.
  10. Production tips.
In the link below find the code as Netbeans project, divided into sections based on topics above, and also includes the material that guides you in details from software pre-requisite to compile and run the code, full explanation of every line of code and testing scenarios.

NIO2 project code and material.


Friday, December 16, 2011

Weblogic: How to setup & test Oracle WebLogic Server 12c (a quick guide On Windows, Linus & Mac OS X)

Weblogic 12c = JEE6 full profile Edition.

Oracle WebLogic 12c is a, certified, full profile Java EE 6 server.

Here is a quick guide for downloading, configuring and installing Oracle weblogic 12c zipped file (size = 164M) development environment.

Steps:
  1. Download size of wls1211_dev.zip is 164 MB.
  2. Installation requires you to unzip the archive, run some scripts and set the following JVM options: -Xmx1024m -XX:MaxPermSize=256m (without setting the PermSize, the deployment didn't work) refer to  QUICKSTART.
  3. Disc size (server): 383 MB after installation
  4. Disc size (domain): 26 MB after installation
  5. Startup: execution of [DOMAIN-FOLDER]/startWebLogic.sh is fast (< 7 secs)
  6. Deployment: copy the ServerSmokeTest.war into [DOMAIN-FOLDER]/standalone/autodeploy/.
For Installation & configuration (On Windows, Linus & Mac OS X) follow the following steps:
The WLS zip distribution is intended for development use. It is supported on  Windows, Linux and Mac OS X systems. It contains the necessary artifacts to develop and test applications on WebLogic Server. 
An optional supplemental zip (wls1211_dev_supplemental.zip) is available as a separate download. The supplemental zip contains samples, evaluation database (Derby) and L10N console help files. 
The following instructions should help in quickly setting up WLS. Please refer to the general WLS documentation for detailed instructions. 
This version of WLS requires JDK 1.6 as a minimum. 
Ensure that you have the proper JDK version installed and ready for use. 

QUICKSTART
---------------------
Note: don't use any spaces in the paths referred in environment variables like JAVA_HOME, MW_HOME etc...
1. Extract the contents of the zip to a directory. This directory is referred to as MW_HOME (eg: /home/myhome/mywls).

2. Setup JAVA_HOME, JAVA_VENDOR, MW_HOME variables in the current shell as required for the target platform.
  
Linux
    $ export JAVA_HOME=/home/myhome/myjavahome
    $ export MW_HOME=/home/myhome/mywls
  
Mac
    $ export JAVA_HOME=
      /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
    $ export USER_MEM_ARGS="-Xmx1024m -XX:MaxPermSize=256m"       
    $ export MW_HOME=/home/myhome/mywls
  
Windows
    > set JAVA_HOME=C:\home\myhome\myjavahome
    > set MW_HOME=C:\home\myhome\mywls
    > set JAVA_VENDOR=[Sun|Oracle] 

  On Windows you must set JAVA_VENDOR to the JVM being used:
    If using HotSpot, use "Sun"
    If using JRockit, use "Oracle"

3. Run the installation configuration script in the MW_HOME directory.

This step is required to be run only once. If you move the installation to another location/machine, you need to rerun this step.

  Linux/Mac: $ ./configure.sh 
  Windows: > configure.cmd

  On Windows, this step retains the WLS environment settings for the current shell. 
  So, the next step can be omitted.

4. Setup WLS environment in the current shell. 
   Linux: $ . $MW_HOME/wlserver/server/bin/setWLSEnv.sh 
   Mac:   $ . $MW_HOME/wlserver/server/bin/setWLSEnv.sh 
   Windows: > %MW_HOME%\wlserver\server\bin\setWLSEnv.cmd

5. Create a new WLS domain and start WLS.
   
It is recommended that you create domains outside the MW_HOME.
   
Linux
    $ mkdir /home/myhome/mydomain
    $ cd /home/myhome/mydomain
    $ $JAVA_HOME/bin/java $JAVA_OPTIONS -Xmx1024m -XX:MaxPermSize=128m weblogic.Server
  
Mac
    $ mkdir /home/myhome/mydomain
    $ cd /home/myhome/mydomain
    $ $JAVA_HOME/bin/java $JAVA_OPTIONS -Xmx1024m -XX:MaxPermSize=256m weblogic.Server
   
Windows
    > mkdir C:\home\myhome\mydomain
    > cd C:\home\myhome\mydomain
    > %JAVA_HOME%\bin\java.exe %JAVA_OPTIONS% -Xmx1024m -X:MaxPermSize=128m weblogic.Server

Once the domain is created, you can shutdown WLS and restart it with the scripts provided in the newly created domain.

Note: You can also create the domain by invoking the GUI configuration wizard (Run MW_HOME/wlserver/common/bin/config.[sh|cmd]).

6. If you already have an existing domain that you want to run with this installation, edit the DOMAIN_HOME/bin/setDomainEnv.sh script and change the WL_HOME to point to the new installation - ${MW_HOME}/wlserver/.

Note: If the existing domain has samples configured, the server will issue failures during startup as samples are not included in the zip distribution.

7. Start a browser and open up url - 'http://localhost:7001/console' to administer the server.

8. If you need samples, evaluation database (Derby) or console help files for non-english locales, you can download the supplemental zip and extract it under MW_HOME. Follow instructions in README_SUPP.txt to properly setup samples. this file can be found in WLS extracted folder.


WHAT IS NOT INCLUDED
---------------------------------------
The following components are not available in the zip distribution.

  • Coherence libraries
  • WebServer plugins
  • Native JNI libraries for unsupported platforms
  • Samples, evaluation database (Derby), non-english console help (can be added by using the WLS supplemental zip)

CLEANUP
---------------
Delete the MW_HOME directory. Note that this will delete everything under MW_HOME. If you have created domains or extracted supplemental zip those files will be removed as well. Ensure that you don't create domains or applications under MW_HOME directory for easier migration. 


UPGRADE
----------------

  • In-place upgrade of installation is not supported in the zip distribution. 
  • Download the new version of zip distribution and extract it to a new location. 
  • Change the MW_HOME to point to the new location and update the startup scripts in your domains to point to the new MW_HOME.

KNOWN ISSUES
--------------------------

  • [MAC OS X] Memory settings have to be explicitly defined using the USER_MEM_ARGS variable prior to running any domain scripts.
  • [MAC OS X] The config wizard does not recognize a Mac OS X JDK installation and issues a warning. But, it successfully creates a domain and should not cause any issue running servers in the domain.
  • [MAC OS X] The zip distribution has limited support for functionality requiring native support. JMS C clients, auto stack-dumps, auto privileged port binding are not supported.
  • [ALL OS] Smart update tool is not supported in a zip distribution.
  • [ALL OS] The zip distribution should not be extracted to an existing MW_HOME
  •   or BEA_HOME which had been created by a regular installers. Doing so will conflict with already installed products and is unsupported.
  • [ALL OS] JAVA_HOME cannot contain directory names with space characters.


General impression:
  1. Installation is a bit more complicated, comparing it to GlassFish, JBoss, TomEE or Siwpas. All Java EE 6 servers so far can be installed just by unzipping the archive. WebLogic requires you to run some scripts after unzipping. However: the installation can be performed in a few minutes.
  2. The server is lean and fast. Startup and deployment performance is good.
  3. The admin console http://localhost:7001/console is fast, clean and usable. WebLogic comes with extensive monitoring capabilities.