21.06.2012

Win32 Basic File Copy

Basic File Copy
  1. // BasicFileCopy.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include<Windows.h>
  6.  
  7.  
  8. int _tmain(int argc, _TCHAR* argv[])
  9. {
  10.     HANDLE hIn, hOut;
  11.     DWORD nIn,nOut;
  12.     CHAR Buffer[BUFSIZ];
  13.  
  14.     //control the arguments
  15.     if(argc != 3)
  16.     {
  17.         printf("Usage : Basic File Copy file1 file2\n");
  18.         return 1;
  19.     }
  20.  
  21.     hIn = CreateFile(argv[1],GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
  22.     if(hIn == INVALID_HANDLE_VALUE)
  23.     {
  24.         printf("Cannot open input file. Error %x\n",GetLastError());
  25.         return 3;
  26.     }
  27.     hOut = CreateFile(argv[2],GENERIC_WRITE,0,NULL,CREATE_ALWAYS,0,NULL);
  28.     if(hIn == INVALID_HANDLE_VALUE)
  29.     {
  30.         printf("Cannot open output file. Error %x\n",GetLastError());
  31.         return 3;
  32.     }
  33.  
  34.     while(ReadFile(hIn,Buffer,BUFSIZ,&nIn,NULL) && nIn > 0)
  35.     {
  36.         WriteFile(hOut,Buffer,nIn,&nOut,NULL);
  37.         if(nIn != nOut)
  38.         {
  39.             printf("Fatal Wrie Error: %x\n",GetLastError());
  40.             return 4;
  41.         }
  42.     }
  43.     CloseHandle(hIn);
  44.     CloseHandle(hOut);
  45.     return 0;
  46. }

8.03.2012

Profiling the commit problem in Spring JDBCTemplate

Spring’s JdbcTemplate simplifies the usage of jdbc and also provides callbacks to create and execute callable statements.

Problem :
For a single sign on project using jasig-cas framework is well known choice to adopt all web based applications. This framework also provides authentcation-extensions to develop custom authentication handlers.

For ticket persistency, jpa transaction manager is configured and the jdbc authentication handler uses a stored procedure and does not commit to database if you are using ticket jpa transaction manager.

Solution :
William Louth who is the founder, CTO of Jinspired and creator of JxInsight always saves my life. For that kind of a problem installing and enabling the transaction analysis is enough to see jdbc low level instrumentations.
Firstly configure jxinsight for tracing jdbc calls, and replace jdbc urls with the jxinsight urls according to manual.

After the configuration, reset jxisnight and try to login from CAS-Login page. In the picture below, the arrow shows that there is rollback and from the jxinsight’s stack table (not in the picture) it is very easy to understand that it is for tomcat’s validation query.
image

Right now the question is where is commit or rollback for the stored procedures. The problem begins here. JPATransactionManager is not controlling jdbcTemplate. This means that  adding one more transaction manager to applicationContext. The first transaction manager is controlling the tickets and the second one is controlling our  jdbc authentication handler. This fixes our commit problem.

7.03.2012

JASIG-CAS Deadlock Problem

Jasig-CAS is a well-known Web Based Single-Sign-Framework developed with almost all Spring features. Basically you can get more information from the documentation.

TicketRegistryCleaner  deletes tickets from datasource based on your configuration. In a High Available (HA) configuration tickets are persisted on databases to serve multiple clients and services with multiple cas-servers. This deletion locks and sometimes casuses deadlocks in oracle since it uses update for. Jasig-Wiki suggests developers to use appropriate locking strategies for their architecture.  Although using JPALocking strategy for HA environments is not enough to solve deadlock problems in jasig-cas.

After working with our oracle database administrators i decided to develop a new TicketRegistryCleaner that is only active in one of cas-servers and just cleans the tickets at midnight 02:00 to 05:00.

This kind of approach is not solving the deadlocks, it  just pushes the deadlock problems to midnights. If it is necessary for the cleanup a stored procedure can be written to delete the unused and timeout tickets.  In the below you will see scheduled registry cleaner source code.

Ticket Registry Cleaner
  1. package com.montoya.sso.ticket.registry.support;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Calendar;
  5. import java.util.Collection;
  6. import java.util.Date;
  7. import java.util.List;
  8.  
  9. import javax.validation.constraints.NotNull;
  10.  
  11. import org.jasig.cas.ticket.Ticket;
  12. import org.jasig.cas.ticket.TicketGrantingTicket;
  13. import org.jasig.cas.ticket.registry.RegistryCleaner;
  14. import org.jasig.cas.ticket.registry.TicketRegistry;
  15. import org.jasig.cas.ticket.registry.support.JdbcLockingStrategy;
  16. import org.jasig.cas.ticket.registry.support.LockingStrategy;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19.  
  20. public class MontoyaTicketRegistryCleaner implements RegistryCleaner {
  21.  
  22.     /** The Commons Logging instance. */
  23.     private final Logger log = LoggerFactory.getLogger(getClass());
  24.     
  25.     @NotNull
  26.     private int startTime;
  27.     
  28.     @NotNull
  29.     private int endTime;
  30.     
  31.  
  32.     /** The instance of the TicketRegistry to clean. */
  33.     @NotNull
  34.     private TicketRegistry ticketRegistry;
  35.  
  36.     /** Execution locking strategy */
  37.     @NotNull
  38.     private LockingStrategy lock;
  39.  
  40.     private boolean logUserOutOfServices = true;
  41.  
  42.     private boolean active;
  43.  
  44.     public void setActive(boolean active) {
  45.         this.active = active;
  46.     }
  47.  
  48.     public void setStartTime(int startTime) {
  49.         this.startTime = startTime;
  50.     }
  51.  
  52.     public void setEndTime(int endTime) {
  53.         this.endTime = endTime;
  54.     }
  55.  
  56.     /**
  57.      * @see org.jasig.cas.ticket.registry.RegistryCleaner#clean()
  58.      */
  59.     public void clean() {
  60.         if (!active) {
  61.             log.info("MontoyaTicketRegistryCleaner is not active on this node");
  62.             return;
  63.         }
  64.  
  65.         int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
  66.         if (hour < startTime || hour > endTime) {
  67.             log.debug("TicketRegistryCleaner is active but it is not working except {} - {} ",startTime,endTime);
  68.             return;
  69.         }
  70.  
  71.         this.log.info("Beginning ticket cleanup.");
  72.         this.log.debug("Attempting to acquire ticket cleanup lock.");
  73.         if (!this.lock.acquire()) {
  74.             this.log.info("Could not obtain lock.  Aborting cleanup.");
  75.             return;
  76.         }
  77.         this.log.debug("Acquired lock.  Proceeding with cleanup.");
  78.         try {
  79.             final List<Ticket> ticketsToRemove = new ArrayList<Ticket>();
  80.             final Collection<Ticket> ticketsInCache;
  81.             ticketsInCache = this.ticketRegistry.getTickets();
  82.             for (final Ticket ticket : ticketsInCache) {
  83.                 if (ticket.isExpired()) {
  84.                     ticketsToRemove.add(ticket);
  85.                 }
  86.             }
  87.  
  88.             this.log.info(ticketsToRemove.size() + " tickets found to be removed.");
  89.             for (final Ticket ticket : ticketsToRemove) {
  90.                 if (this.logUserOutOfServices && ticket instanceof TicketGrantingTicket) {
  91.                     ((TicketGrantingTicket) ticket).expire();
  92.                 }
  93.                 this.ticketRegistry.deleteTicket(ticket.getId());
  94.             }
  95.         } finally {
  96.             this.log.debug("Releasing ticket cleanup lock.");
  97.             this.lock.release();
  98.         }
  99.  
  100.         this.log.info("Finished ticket cleanup.");
  101.     }
  102.  
  103.     /**
  104.      * @param ticketRegistry
  105.      *            The ticketRegistry to set.
  106.      */
  107.     public void setTicketRegistry(final TicketRegistry ticketRegistry) {
  108.         this.ticketRegistry = ticketRegistry;
  109.     }
  110.  
  111.     /**
  112.      * @param strategy
  113.      *            Ticket cleanup locking strategy. An exclusive locking strategy
  114.      *            is preferable if not required for some ticket backing stores,
  115.      *            such as JPA, in a clustered CAS environment. Use
  116.      *            {@link JdbcLockingStrategy} for
  117.      *            {@link org.jasig.cas.ticket.registry.JpaTicketRegistry} in a
  118.      *            clustered CAS environment.
  119.      */
  120.     public void setLock(final LockingStrategy strategy) {
  121.         this.lock = strategy;
  122.     }
  123.  
  124.     /**
  125.      * Whether to log users out of services when we remove an expired ticket.
  126.      * The default is true. Set this to false to disable.
  127.      *
  128.      * @param logUserOutOfServices
  129.      *            whether to log the user out of services or not.
  130.      */
  131.     public void setLogUserOutOfServices(final boolean logUserOutOfServices) {
  132.         this.logUserOutOfServices = logUserOutOfServices;
  133.     }
  134.  
  135. }

Eclipse : Java was started but returned exit code = 13

image

If you get the exception "Eclipse Java was started but returned exit code = 13" then  this means you installed x86 version of eclipse on an x64 environment and eclipse.ini is using wrong version of jdk. Eclipse and jdk  build architecture have to be same. Just showing the right jdk build architecture path from eclipsi.ini fixes this problem.

6.12.2010

BSOD based on VmWare Network Driver

This week I was in IBM’s place to learn WPS. In the second morning of that course  my windows box crashed with a BSOD. The IBM guys claimed it was normal to windows to do that. To be honest I’m not a Microsoft fan but I trust windows’ stability. So at that moment I started to analyze the core dump which was generated by this crash. . In the picture below you’ll see that vmware network driver caused windows to crash. Windows was not crashed because of itself Smile
BSOD

7.09.2010

How to disable Security in Websphere Application Server 7

When working on WAS you can enable and disable security for testing purposes. A few times i configured WAS security and could not logined to the system. If there is a need to disable security on WAS you can disable by setting the property enabled to false.
<was_installation_dir>/profiles/<profileName>/config/cells/cell_name

<security:Security xmi:version="2.0" 
xmlns:xmi="http://www.omg.org/XMI"
xmlns:orb.securityprotocol="http://www.ibm.com/websphere/appserver/schemas/5.0/orb.securityprotocol.xmi"
xmlns:security="http://www.ibm.com/websphere/appserver/schemas/5.0/security.xmi"
xmi:id="Security_1" useLocalSecurityServer="true"
useDomainQualifiedUserNames="false"
enabled="true"cacheTimeout="600" issuePermissionWarning="true"
activeProtocol="BOTH" enforceJava2Security="false"
enforceFineGrainedJCASecurity="false" appEnabled="true"
dynamicallyUpdateSSLConfig="true" allowBasicAuth="true"
activeAuthMechanism="LTPA_1"
activeUserRegistry="LDAPUserRegistry_1"
defaultSSLSettings="SSLConfig_GMCIMIT891Node02_1" adminPreferredAuthMech="RSAToken_1">



6.09.2010

Using TCP/IP Monitor Plug-in in Eclipse

Eclipse has various types of plug-ins. In this post you’ll find out how to configure tcp/ip monitor plugin  in eclipse.
Eclipse’s TCP/IP Monitor works like a proxy server which configures itself in between client and server.
1.Configure TCP/IP Monitor listening port which is not used for an application. This port will your gateway for your requests. So In the picture below you can assume that an application is working on tcp:9080.
TCPIPProp
2. Send requests from 9081
SoapUI
3. Investigate your requests.
Requests