Thursday, September 17, 2015

ReactiveX in Java

Dependency:

<dependency>
    <groupId>io.reactivex</groupId>
    <artifactId>rxjava</artifactId>
    <version>1.0.14</version>
</dependency>

Code:

Observable:

ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 5, 3000L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue());

Observable<ChannelItemType> myObservable = Observable.create(
        new Observable.OnSubscribe<ChannelItemType>() {
            @Override
            public void call(Subscriber<? super ChannelItemType> sub) {
                sub.onNext(item);
                sub.onCompleted();
            }
        }
).subscribeOn(Schedulers.from(executor));
myObservable.subscribe(new ChannelConsumer(channelRepo));

Above snippet, we create an Observable, sent item on onNext() function to Consumer (in this case ChannelConsumer). One interesting thing here is we thread consuming by using executor. We create 3 thread in executor. Obervable automatically pick thread in executor and send data.

Consumer:
public class ChannelConsumer extends Subscriber<ChannelItemType> {
    @Override
    public void onCompleted() {
        //Clean up your process when complete
    }
    @Override
    public void onError(Throwable throwable) {

    }
    @Override
    public void onNext(ChannelItemType channelItem) {
        //put your code here
    }
}

Thursday, August 20, 2015

Monday, August 17, 2015

Make Virtualbox display full Linux client


References:
https://help.ubuntu.com/community/VirtualBox/GuestAdditions
http://www.binarytides.com/vbox-guest-additions-ubuntu-14-04/

Also:  Try going to Software and Updates -> Additional Drivers and choose: Use x86 visualization solution

After all remember choosing: View -> Auto-resize Guest Display

Batch insert with JDBC and Spring

JDBC:

String sql = "insert into employee (name, city, phone) values (?, ?, ?)";
Connection connection = new getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
final int batchSize = 1000;
int count = 0;
for (Employee employee: employees) {
     ps.setString(1, employee.getName());
     ps.setString(2, employee.getCity());
     ps.setString(3, employee.getPhone());
     ps.addBatch();

     if(++count % batchSize == 0) {
          ps.executeBatch();
     }
}
ps.executeBatch(); // insert remaining records

ps.close();
connection.close();

Spring Data:

public class EmployeeRepository extends JdbcDaoSupport { 

    public void insertEmployeeBatch(List<Employee> employees) {

        try {
            String sql = "
insert into employee (name, city, phone) values (?, ?, ?)";

            getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                     Employee employee = employees.get(i);                    

                     ps.setString(1, employee.getName());
                     ps.setString(2, employee.getCity());
                     ps.setString(3, employee.getPhone());
                }

                public int getBatchSize() {
                    return
employees.size();
                }
            });
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
        }
    } 

}

Thursday, August 13, 2015

Read big XML file in Java

Tool:

LTFViewer

Maven:

<dependency>
        <groupId>org.codehaus.woodstox</groupId>
        <artifactId>stax2-api</artifactId>
        <version>3.1.2</version>
</dependency>

Java:

public void parseFile(String filename, Long jobId) throws Exception {
        XMLStreamReader2 xmlr = null;
        FileInputStream is = null;
        List<Future> trackingTask = null;

        try {
            XMLInputFactory2 xmlif = ((XMLInputFactory2) XMLInputFactory.newInstance());
            xmlif.configureForSpeed();

            // Start init executor service
            trackingTask = initExecutorService();

            is = new FileInputStream(filename);
            xmlr = (XMLStreamReader2) xmlif.createXMLStreamReader(is);

            // Parse into typed objects
            JAXBContext ctx = JAXBContext.newInstance(PGWSSchedulePOJO.class, PGWSChannelPOJO.class...);
            Unmarshaller um = ctx.createUnmarshaller();
            while (xmlr.hasNext()) {
                xmlr.next();
                if (xmlr.isStartElement()) {
                    if ((xmlr.getLocalName().equals("publishedTitles"))) {
                        NlpgwsTitleInfoPOJO ti = um.unmarshal(xmlr, NlpgwsTitleInfoPOJO.class).getValue();
                        if (ti != null) {
                            //Add to queue "parsedTitleInfo"
                            parsedTitleInfo.add(ti);
                        }
                    } else if ((xmlr.getLocalName().equals("providers"))) {
                        NlpgwsProviderPOJO pr = um.unmarshal(xmlr, NlpgwsProviderPOJO.class).getValue();
                        if (pr != null) {
                            //Add to queue "parsedProviders"
                            parsedProviders.add(pr);
                        }
                    }
                }
            }

        } catch (XMLStreamException ex) {
            LOGGER.error(ex.getMessage(), ex);
            throw ex;
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
            throw ex;
        } finally {
            if (xmlr != null) {
                try {
                    xmlr.close();
                } catch (Exception ex) {
                    LOGGER.error(ex.getMessage());
                }
            }

            if (is != null) {
                try {
                    is.close();
                } catch (Exception ex) {
                    LOGGER.error(ex.getMessage());
                }
            }
        }

        // Waiting for all task to completed and shutdown the executor server
        try {
            if (trackingTask != null) {
                for (Future future : trackingTask) {
                    future.get();
                }
            }
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
        } finally {
            // clean job
        }
    }

With:
protected static BlockingQueue<NlpgwsProviderPOJO> parsedProviders = new LinkedBlockingDeque<NlpgwsProviderPOJO>();
    protected static BlockingQueue<NlpgwsTitleInfoPOJO> parsedTitleInfo = new LinkedBlockingDeque<NlpgwsTitleInfoPOJO>();

Algorithm here:
- Read XML file
- Put data object to a BlockingQueue
- Init Executor -> create thread to scan the Blocking queue to do the business logic

Declare executor:
protected ExecutorService ingestionBatchInsert;
ThreadFactory batchInsertThreadFactory = new ThreadFactoryBuilder().setNameFormat("dls-batch-insert-%d").build();
ingestionBatchInsert = Executors.newFixedThreadPool(totalInsertThread + 1, batchInsertThreadFactory);

Add thread in trackingList:
for (int count = 0; count < totalInsertThread; count++) {
      Future task = ingestionBatchInsert.submit(othersBatchInsertThread);
      trackingTask.add(task);
}

OthersBatchInsertThread thread:
public class OthersBatchInsertThread implements Runnable {
        public void run() {
              //Get out object from BlockingQueue
             DLSFileParserService.parsedProviders.poll()
             //process
        }
}

Check file is open (lock) or not in Java

RandomAccessFile rf = new RandomAccessFile(file, "rw");
FileChannel fileChannel = rf.getChannel();
FileLock lock = null;
try {
        // let us try to get a lock. If file already has an exclusive lock by another process
        LOGGER.info("Trying to acquire lock");
        lock = fileChannel.tryLock();
        if (lock != null) {
              success = true;
        }
} catch (Exception ex) {
         LOGGER.error(ex.getMessage());
} finally {
         if (lock != null) {
               lock.release();
         }
         if(fileChannel != null){
               fileChannel.close();
         }
          if(rf != null){
               rf.close();
          }
}

Observe folder to pick file when it's available


public class MMTServerStartListener implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    YourMonitorListenerImpl fileMonitor;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent context) {
        try {
            String filePath = "<file path>";
            startMonitor(filePath, fileMonitor);
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
        }
    }

    private void startMonitor(String filePath, YourMonitorListenerImpl fileMonitorImpl) {
        try {
            if (filePath != null && filePath.length() > 0) {

                final File directory = new File(filePath.trim());
                FileAlterationObserver fao = new FileAlterationObserver(directory);
                fao.addListener(fileMonitorImpl);

                final FileAlterationMonitor monitor = new FileAlterationMonitor();
                monitor.addObserver(fao);

                LOGGER.info("Starting monitor. CTRL+C to stop.");
                monitor.start();

                Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
                    public void run() {
                        try {
                            LOGGER.info("Stopping monitor.");
                            monitor.stop();
                        } catch (Exception ignored) {
                            LOGGER.error(ignored.getMessage(), ignored);
                        }
                    }
                }));
            } else {
                LOGGER.error("Invalid input the monitor folder");
            }
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
        }
    }
}

With "YourMonitorListenerImpl" implements "FileAlterationListener" interface.

Wednesday, August 12, 2015

Thread with ExecutorService

ThreadFactory buildCache = new ThreadFactoryBuilder().setNameFormat("thread-name-%d").build();
ExecutorService executorService = Executors.newFixedThreadPool(totalThreads, buildCache);

List<Future> trackingTask = new ArrayList<Future>();

for (int index = 0; index < totalThreads; index++) {
        trackingTask.add(executorService.submit(new Runnable() {
              @Override
               public void run() {
                      //run your code
               }
        }));
}

// Run and waiting for task to finish
for (Future task : trackingTask) {
        task.get();
}

// Shutdown executor:
executorService.shutdownNow();

Using Redis cache

Installation:

Follow the instruction at: Install Redis cache in linux - DigitalOcean

Using Redis cache:

Create bean:
<bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:hostName="${atm.cache.redis.host}"
          p:port="${atm.cache.redis.port}"
          p:poolConfig-ref="jedisPoolConfig"
          p:usePool="true"/>
 <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
          p:connectionFactory-ref="jedisConnFactory" p:keySerializer-ref="stringRedisSerializer"/>

 Autowired in java implement:
@Autowired
private RedisTemplate<String, ProgramBasicInfo> programRedisTemplate;

Delete all cache: (in this case, it get connection and then flush all data)
programRedisTemplate.getConnectionFactory().getConnection().flushAll();

Delete cache:
programRedisTemplate.opsForHash().getOperations()
                                    .delete(Constants.CACHE_PROGRAM_GUIDE_KEY);

Put data to cache:
programRedisTemplate.opsForHash().put(Constants.CACHE_PROGRAM_GUIDE_KEY, program.getProgramId(),       programBasicInfo);

Get data from cache:
(ProgramBasicInfo) programRedisTemplate.opsForHash().get(Constants.CACHE_PROGRAM_GUIDE_KEY, programId);

References:

http://blog.joshuawhite.com/java/caching-with-spring-data-redis/
http://caseyscarborough.com/blog/2014/12/18/caching-data-in-spring-using-redis/


Tuesday, August 11, 2015

Synchronize SimpleDateFormat object in Java

SimpleDateFormat object does not work properly in a multi threaded environment. It may output a wrong date when parsing. So the safest way is to synchronize it.

private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public String formatDate(Date d) {
    synchronized(sdf) {
        return sdf.format(d);
    }
}
 
Hope that help.

Sunday, August 9, 2015

Check if a class is loaded and lib file location in JVM

Example below is to see if JVM using ojdbc6 version 11 or version 12.

final ClassLoader loader = Thread.currentThread().getContextClassLoader();
ClassPath clazzPath = ClassPath.from(loader);
Set<ClassInfo> classes = clazzPath.getTopLevelClasses();
for (final ClassPath.ClassInfo classInfo : classes) {
    if (classInfo.getName().startsWith("oracle")) {
         if (classInfo.getName().contains("oracle.jdbc.babelfish")) {
                 isVersion12 = true;
         }

    }
}
            

if(isVersion12) {
        LOGGER.info("Ojdbc version 12");

} else {
        LOGGER.info("Ojdbc version 11");

}

Class klass = OracleConnection.class;
URL location = klass.getResource('/' + klass.getName().replace('.',  '/') + ".class");
LOGGER.info(location.toString());

Read package name and version from Manifest file

Bean: 

public class BuildVersion {
   
    /** The build name. */
    private String buildName;
   
    /** The version. */
    private String version;

    /**
     * Gets the version.
     *
     * @return the version
     */
    public String getVersion() {
        return version;
    }

    /**
     * set version.
     *
     * @param version the new version
     */
    public void setVersion(String version) {
        this.version = version;
    }

    /**
     * Gets the builds the name.
     *
     * @return the builds the name
     */
    public String getBuildName() {
        return buildName;
    }

    /**
     * Sets the builds the name.
     *
     * @param buildName the new builds the name
     */
    public void setBuildName(String buildName) {
        this.buildName = buildName;
    }
}

VersionServiceImpl:

private BuildVersion getBuildVersion() {
        BuildVersion buildVersion = new BuildVersion();

        Class clazz = this.getClass();
        String className = clazz.getSimpleName() + ".class";
        String classPath = clazz.getResource(className).toString();
        try {
            String manifestPath = classPath.substring(0, classPath.lastIndexOf("/WEB-INF")) +
                    "/META-INF/MANIFEST.MF";
            Manifest manifest = new Manifest(new URL(manifestPath).openStream());
            Attributes attr = manifest.getMainAttributes();
            String title = attr.getValue("Specification-Title");
            String version = attr.getValue("Specification-Version");

            buildVersion.setBuildName(title);
            buildVersion.setVersion(version);
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
        }
        return buildVersion;
    }

VersionEndpoint:

@Component
@Path ("/")
public class VersionEndpoint {
   
    /** The version service. */
    @Autowired
    private VersionService versionService;
   
    /**
     * getVersion service use to retrieve the current release version.   
     *
     * @param format the format
     * @return Version response
     */
    @GET
    @Path ("/version")
    @Produces ({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public Response getVersion(@DefaultValue (WSConstants.RESPONSE_TYPE_JSON) @QueryParam ("format") String format) {
        try {
            return Response.ok().entity(versionService.getVersion()).type(WSUtil.getWSResponseType(format)).build();
        } catch (Exception e) {
            return Response.ok().entity("Error: "  + e.getMessage()).type(WSUtil.getWSResponseType(format)).build();
        }       
    }                                       
}

Install Ibus-unikey in Linux for Vietnamese

Uninstall all preinstalled ibus package and its dependencies
    sudo apt-get remove --auto-remove ibus
Purging config/data too
    sudo apt-get purge --auto-remove ibus
Maybe restart is needed.

Install new ibus-unikey
    sudo apt-get install ibus-unikey

After installing done, run:
    ibus-setup

to open Ibus Preferences. 
Choose Input method tab, check 'Customize active input methods'. In selection box, click Show all input methods, choose Vietnamese->Unikey.

Logout your system to active the new input method.

Now in top right corner of your screen, you are able to select 'Unikey' to input Vietnamese.

Thursday, August 6, 2015

Some good Linux scripts

Change test in file

sed -i s/helloword/HELLOWORLD/g file.txt

Copy file from Linux server to Linux server

scp source_file_name username@destination_host:destination_folder
     or
scp [-r] [[user1@]hostname1:]file1 ... [[user2@]hostname2:]file2

Ex:
scp pluto:/home/jones/letter.doc .
copy the file letter.doc from the /home/jones directory of the remote system pluto to the working directory on the local system 

scp notice.doc pluto:/home/jones
copy the file notice.doc from current directory of the local system to the /home/jones directory of the remote system, pluto

Find file match name in a folder

cd <your-folder>
for f in *; do
        case $f in
                abc*.xml)
                        echo $f
                        ;;
               
def*.xml)
                        echo $f
                        ;;
        esac
done


Check file is open in linux

Check if file prefix by 'filename' is open by 'cp' process:

while :
do
    if [[ `lsof -c cp | grep filename*.xml` ]]
    then
    printf ".";
    sleep 0.5
       
    else
        break   
    fi
done
    echo "Done!"

Auto deploy script for Weblogic

Some Configuration (deploy.conf)

#Enter the package location here
source=/data/Jenkins_builds/MMT
#Enter the server IP here
FEServer=<fronend-ip-server>
BEServer=<backend-ip-server>

#Enter the target name here
FETarget=Server-0
BETarget=Server-2

#Enter package name
FEPackage=atm-ui-portlet
BEPackage=mmt-ws

#Enter the source path here
FEPath=/data/deployment/portal_destination
BEPath=/data/deployment

#Enter the portal deploy path here
PortalDeploy=/data/deployment/portal_deploy

Auto deploy script

#!/bin/bash
Logfile="logAutoDeploy.txt"

. ./deploy.conf

echo [$(date)]: ============Starting to auto deploy========== >> $Logfile

if [ -f $source/$BEPackage.war ]; then
    echo [$(date)]: ============Deploy Backend package========== >> $Logfile
    echo undeploy the package $BEPackage
    java -classpath /data/weblogic/wlserver_10.3/server/lib/weblogic.jar  weblogic.Deployer -adminurl t3://$BEServer:7001 -user <user-name> -password <password> -undeploy -name $BEPackage
    echo the package $BEPackage is undeployed >> $Logfile

    echo delete the package $BEPath/$BEPackage >> $Logfile
    cd $BEPath
    rm -rf $BEPackage

    mkdir $BEPackage
    cd $BEPackage

    echo copy $BEPackage package to $BEPath/$BEPackage >> $Logfile
    cp $source/$BEPackage.war ./
   
    echo unzip $BEPackage package >> $Logfile
    unzip $BEPackage.war
    rm -f $BEPackage.war
    echo deploy $BEPackage to back end server >> $Logfile
    java -classpath /data/weblogic/wlserver_10.3/server/lib/weblogic.jar  weblogic.Deployer -adminurl t3://$BEServer:7001 -user <user-name> -password <password> -deploy $BEPath/$BEPackage -targets $BETarget

    echo remove $BEPackage war file in Jenkins build >> $Logfile
    cd $source
    mv $BEPackage.war tmp
fi

if ls $source/$FEPackage* 1> /dev/null 2>&1; then
    echo [$(date)]: ============Deploy Frontend package========== >> $Logfile
    echo undeploy the package $FEPackage >> $Logfile
    java -classpath /data/weblogic/wlserver_10.3/server/lib/weblogic.jar  weblogic.Deployer -adminurl t3://$FEServer:7001 -user <user-name> -password <password> -undeploy -name $FEPackage
    echo the package $FEPackage is undeployed >> $Logfile

    echo delete the package $FEPackage >> $Logfile
    cd $FEPath
    rm -rf $FEPackage*

    echo copy the $FEPackage to $PortalDeploy >> $Logfile
    scp $source/$FEPackage* $PortalDeploy
    echo waiting to Liferay extracts the package >> $Logfile
    sleep 10
      
    echo deploy $FEPackage to server >> $Logfile
    java -classpath /data/weblogic/wlserver_10.3/server/lib/weblogic.jar  weblogic.Deployer -adminurl t3://$FEServer:7001 -user <user-name> -password <password> -deploy $FEPath/$FEPackage -targets $FETarget   

    echo remove $FEPackage war file in Jenkins build >> $Logfile
    cd $source
    mv $FEPackage* tmp
fi

Tuesday, June 23, 2015

Import/Export large data in Oracle DB

Export:

C:\Oracle\Database\product\11.2.0\dbhome_1\BIN>exp test/123456@db_name1 file=export_data.dump log=log.txt buffer=10485867 statistics=none tables=(table1,table2)


Import:

C:\Oracle\Database\product\11.2.0\dbhome_1\BIN>imp test/123456@db_name1 file=export_data.dump full=yes


Using Pump Export/Import

    Export:
    expdp scott/tiger DIRECTORY=dmpdir DUMPFILE=scott.dmp
    with "dmpdir" created by:
    CREATE DIRECTORY dmpdir AS '/opt/oracle';
    Import:
    impdp system/oracle DIRECTORY=dmpdir DUMPFILE=scott.dmp 
                        REMAP_SCHEMA=<OLD_SCHEMA>:<NEW_SCHEMA>

Monday, June 22, 2015

Increase heap memory in Oracle DB for data import/export

You may encounter "Out of memory" when copy Database or import/export data in SQL Developer tool, go to:

<oracle_product_domain>sqldeveloper\sqldeveloper\bin

Open file: sqldeveloper.conf, add more line:

AddVMOption -Xmx1024M

(defaullt is 128M)

Sunday, May 3, 2015

Fix bug Eclipse hangs Tomcat "starting" status nonstop when using proxy

If you have configured a proxy that does not resolves "localhost", the Tomcat startup will hang in Eclipse. To fix it, go to:

Window -> Preferences -> General -> Network Connections

and see if one of the boxes is checked. Change Active Provider to Direct (it unchecks all boxes) will resolved the problem.

References:
http://stackoverflow.com/questions/11371393/tomcat-not-starting-through-eclipse-timeout

Tuesday, March 17, 2015

Fix DB Network Adapter could not establish

Fist, make sure you configure listener.ora correctly:

# listener.ora Network Configuration File: C:\Oracle\Database\product\11.2.0\dbhome_1\network\admin\listener.ora
# Generated by Oracle configuration tools.

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = CLRExtProc)
      (ORACLE_HOME = C:\Oracle\Database\product\11.2.0\dbhome_1)
      (PROGRAM = extproc)
      (ENVS = "EXTPROC_DLLS=ONLY:C:\Oracle\Database\product\11.2.0\dbhome_1\bin\oraclr11.dll")
    )
  )

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
      (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
      (ADDRESS = (PROTOCOL = TCP)(HOST = 301KhiemTT-W7)(PORT = 1521))

    )
  )

ADR_BASE_LISTENER = C:\Oracle\Database

Run lsnrctrl start to start Listener.

Hope that help!

Monday, March 16, 2015

Receive parameter from AJAX call in Liferay

One very important thing you should noted is set these property in portlet definition (in liferay-portlet.xml) at the end of <portlet> tag:

<add-default-resource>true</add-default-resource>

and

<requires-namespaced-parameters>false</requires-namespaced-parameters>

under <icon> tag.

If not, you can not get parameter's value out in  Portlet Implementation.

Liferay Scheduler

Very good Liferay Scheduler post:
http://itsliferay.blogspot.com/2012/08/implement-scheduler-in-liferay-61.html
https://liferayazam.wordpress.com/2012/06/10/create-a-scheduler-in-liferay-6/
http://pmkathiria.blogspot.com/2013/06/how-to-write-simplecron-scheduler-in.html

Thursday, March 12, 2015

Get autoId from sequence in Oracle when insert data

CREATE SEQUENCE <sequence_name> START WITH 1 INCREMENT BY 1;

INSERT INTO Customers (AutoId, CustomerName, Country)
SELECT <sequence_name>.NEXTVAL, SupplierName, Country FROM Suppliers

Friday, March 6, 2015

Using servlet in Liferay portlet

Create a simple Servlet:

public class MyServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
   
    public MyServlet() {
        super();
    }

    public void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("inside servlet");
    }

}


In web.xml, add:

    <servlet>
        <servlet-name>My Servlet</servlet-name>
        <servlet-class>com.example.servlet.MyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>My Servlet</servlet-name>
        <url-pattern>/myServlet</url-pattern>
    </servlet-mapping>


Remember url path to servlet is: /plugin-context-path/myServlet, not just /myServlet.

Monday, February 9, 2015

Using SVN in Eclipse

Install SVN in Eclipse

Go Help -> Martketplace. Find: "SVN Team provider". Select "Subversive - SVN Team Provider" in result list. Click Install. Follow its instruction and restart Eclipse after finishing.

Go Window -> Perferences -> Team -> SVN. Choose SVN Connect tab. If SVN Connector select box is empty, click to install a new one. Choose SVNKit with latest build. Restart Eclipse if needed.

Using SVN

Checkout your SVN project to your local workspace. Import it to Eclipse. SVN Connector will automatically detect and connect to your SVN repository.

Happy coding!


Thursday, February 5, 2015

Custom JSON web service in Liferay

We can publish JSON webservice from Liferay portlet or hook by 2 ways (at my knowledge at the time of writing this code).

Way No. 1: using Service builder

 Specify "remote-service="true"" in service.xml like below:

<entity name="Employee" local-service="true" remote-service="true">

and run Liferay->SDK->build-service.
It automatically create some interfaces and classes named: *Service, *ServiceImpl or *ServiceUtil... (These files differ with *LocalService, * LocalServiceImpl or *LocalServiceUtil which are used internally JVM).

JSON service has not exposed yet until you write some functions in *ServiceImpl and run Liferay->SDK->build-service again.

The magic is clearly explained in the websites listed in References. Basically, it will add @JSONWebService to *Service interface. By this way, Liferay can scan and publish function in this interface as an API webservice.

Test it by type: http://your-ip:port/plugin-context-path/api/jsonws. It will list out all your service functions that has been published.

Way No. 2: write custom classes and register to Liferay manually

Liferay portal 6.1 is slightly different with Liferay portal 6.2 in the way it scans service functions.

  • In Liferay 6.1:
Create an interface in your package. One thing should be noted is that this interface has to be suffix by "Service" like: "HelloworldService" and mark with @JSONWebService at class scope definition.

@JSONWebService
public interface HelloworldService {
    public java.lang.String sayHello();
}


Corresponding to Service inferface, Liferay need a "Util" class. It will be the endpoint that catches the request from client.

public class HelloworldServiceUtil {
    public static String sayHello() {
        HelloworldServiceImpl service = null;
        if (service == null) {
            HelloworldServiceImpl invokableLocalService = (HelloworldServiceImpl) PortletBeanLocatorUtil.locate("ws-test-portlet", "hellworld");

            if (invokableLocalService != null) {
                service = invokableLocalService;
            }           
        }
       
        if(service != null) {
            return service.sayHello();
        } else {
            return "null";
        }
    }
}


In this case, HelloworldServiceImpl does not take part in directly to the process of  public JSON service. HelloworldService is used to create signature. HelloworldServiceUtil is to handle request. It calls HelloworldServiceImpl to do the business logic and return the result. As you can see, we can get an instance of "HelloworldServiceImpl" from ext-spring.xml through PortletBeanLocatorUtil.locate().
Noted that functions in HelloworldServiceUtil are all static.

Test it by type: http://your-ip:port/plugin-context-path/api/jsonws.

  • In Liferay 6.2:
Things become simpler. Just create an interface and name it any thing you like.

public interface Helloworld {
    public String sayHello();
}


Implement it by an implementation class:

@JSONWebService
public class HelloworldImpl implements Helloworld {

    @Override
    public String sayHello() {
        return "Hello baby";
    }
}


But put @JSONWebService at implementation class like above.
That's all you have to do.

Both in Liferay 6.1 and In Liferay 6.2, you need add JSON Service servlet in web.xml file:

<filter>
        <filter-name>Secure JSON Web Service Servlet Filter</filter-name>
        <filter-class>com.liferay.portal.kernel.servlet.PortalClassLoaderFilter</filter-class>
        <init-param>
            <param-name>filter-class</param-name>
            <param-value>com.liferay.portal.servlet.filters.secure.SecureFilter</param-value>
        </init-param>
        <init-param>
            <param-name>basic_auth</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>portal_property_prefix</param-name>
            <param-value>jsonws.servlet.</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>Secure JSON Web Service Servlet Filter</filter-name>
        <url-pattern>/api/jsonws/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>JSON Web Service Servlet</servlet-name>
        <servlet-class>com.liferay.portal.kernel.servlet.PortalClassLoaderServlet</servlet-class>
        <init-param>
            <param-name>servlet-class</param-name>
            <param-value>com.liferay.portal.jsonwebservice.JSONWebServiceServlet</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>JSON Web Service Servlet</servlet-name>
        <url-pattern>/api/jsonws/*</url-pattern>
    </servlet-mapping>


Try and happy coding!
-----
References:

http://www.liferay.com/documentation/liferay-portal/6.1/development/-/ai/json-web-services

http://www.liferaysavvy.com/2013/11/liferay-custom-json-web-services-on.html

http://www.endeios.io/blog/-/blogs/leverage-liferay-service-s-structure-for-you-own-personal-back-end-s-service

Good Liferay and weblogic sharing

http://bestjavasolutions.blogspot.com/

Wednesday, February 4, 2015

Rest Template handle gzip format

Create new converter for Gzip format:

public class GzipMarshallingHttpMessageConverter extends
        AbstractHttpMessageConverter<DLSSeries> {

    public GzipMarshallingHttpMessageConverter() {
        super(new MediaType("application", "json"));
    }
   
    @Override
    protected boolean supports(Class<?> clazz) {
        return true;
    }

    @Override
    protected DLSSeries readInternal(Class<? extends DLSSeries> clazz,
            HttpInputMessage inputMessage) throws IOException {
        boolean gzip = false;
        HttpHeaders headers = inputMessage.getHeaders();
        List<String> contentEncoding = headers.get("Content-Encoding");

        if (headers != null && contentEncoding != null) {
            for (String contentEncodingStr : contentEncoding) {
                if (contentEncodingStr != null
                        && "gzip".equalsIgnoreCase(contentEncodingStr.trim())) {
                    gzip = true;
                }
            }
        }

        if (gzip) {
            return parseGzipStream(inputMessage.getBody());
        }

        return null;
    }
   
    @Override
    protected void writeInternal(DLSSeries t, HttpOutputMessage outputMessage)
            throws IOException, HttpMessageNotWritableException {
        writeToResult(t, outputMessage.getHeaders(), new StreamResult(
                outputMessage.getBody()));

    }
   
    private DLSSeries parseGzipStream(InputStream inputStreamGzip) {
        DLSSeries series = new DLSSeries();
        List<DLSEpisode> episodeList = new ArrayList<DLSEpisode>();
        try {
            InputStream inputStream = new GZIPInputStream(inputStreamGzip);
            ObjectMapper mapper = new ObjectMapper();
            episodeList = mapper.readValue(inputStream, new TypeReference<ArrayList<DLSEpisode>>() {});
            series.setEpisodeList(episodeList);
        } catch (IOException e) {
            LOGGER.error(e.getMessage());
        }
        return series;
    }

    protected void writeToResult(Object o, HttpHeaders headers, Result result)
            throws IOException {
    }
   
    public String convertStreamToString(InputStream is) throws IOException {
        /*
         * To convert the InputStream to String we use the Reader.read(char[]
         * buffer) method. We iterate until the Reader return -1 which means
         * there's no more data to read. We use the StringWriter class to
         * produce the string.
         */
        if (is != null) {
            Writer writer = new StringWriter();

            char[] buffer = new char[1024];
            try {
                Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            return writer.toString();
        } else {
            return "";
        }
    }
   
    /** Tracking system operations. */
    private static final Logger LOGGER =  LoggerFactory.getLogger(GzipMarshallingHttpMessageConverter.class);
}


Create RestTemplate instance and add new converter to it:

        RestTemplate restTemplate = new RestTemplate();
        List<HttpMessageConverter<?>> messageConverters = new         ArrayList<HttpMessageConverter<?>>();
        messageConverters.add(new GzipMarshallingHttpMessageConverter());

        restTemplate.setMessageConverters(messageConverters);


Using restTemplate, for example:

            ResponseEntity<DLSSeries> response = null;
           
            try {
                response = restTemplate.exchange(
                        url, HttpMethod.GET, entity, DLSSeries.class);
               
                if(response != null && response.getBody() != null) {
                    DLSSeries series = response.getBody();
                   //do more things here
                }
            } catch(RestClientException e) {
                LOGGER.error(e.getMessage());
            }


P/s: change some example classes to your own classes.

Wednesday, January 28, 2015

Maven good book

http://books.sonatype.com/mvnex-book/pdf/mvnex-pdf.pdf

PMD in Eclipse

Install

1.       In Eclipse, click on Help -> Install New Software...
2.       Click on Add.. Enter the following:
3.       Name: PMD for Eclipse Update Site
5.       You should see PMD for Eclipse 3. Select the checkbox next to it and click Next >
Usage
  1. Open new Perspective > Choose PMD
  2.  Open any file and right click > PMD > Check code with PMD
    Violation outline will list out all of the warnings.
    Navigate to the location of the code by double clicking on the warning.
Import Rule

1.       Go to Preferences > PMD > Rules Configuration
2.       Hit 'Clear All' to remove all the default rules.
3.       Import your rule file and click Ok