Design Considerations for Audit Trail Implementation

Auditing the operations in any application is a very common requirement for security and auditing purpose. Auditing can be done at various levels with varying level of details. Overall prime requirements for Audit Trail are:
  • Collect enough information to determine, 
    • Who has done the changes, 
    • What are the changes, 
    • When these changes have been done, Locale etc
    • What user scenario was in action, i.e. the operation context
  • To capture all user actions, even if some of these are not reaching to DB or would be interacting with external services directly (rare, but happens in some cases)
  • Successful and unsuccessful logons or other security related operations
  • Management of collected audit data, which could be very large. 
  • Search operations on collected data
  • Display the collected data to Users in presentable format as and when demanded 

Considering above requirements, there are various design approaches to implement the audit trail in system. Few of these are given below (considering a JEE application, however, many of these are generic)

  1. Implement DB triggers to capture any change in data states and log in audit tables
  2. Log audit data in business services, which means collecting the required data in each business service operation and log it using some abstract Audit service to some data store
  3. Using generic logging framework like Log4J to log the auditing information to desired format and data store and later parse these logs to extract the required data
  4. Interceptors based approach to intercept all operations, collect the data and log it through Audit service. Here interceptors can be of DAO layer interceptors like Hibernate Interceptors or Service level interceptor using AOP.

All of these approaches have their own pros and cons. As obvious, one approach can not be fit for all. Application state, project requirements and scope can define the right set of implementation. So lets discuss the pros and cons of each of these approaches to understand it more.

Database triggers

Database triggers can do really well and nothing can escape from the triggers. Once these are set carefully on all levels, all changes are bound to pass through these and hence will be captured in audit tables.

Pros:

  • Whether it is simple user operation, or any change is being done by DB admin; all kind of changes will be captured by triggers.
  • A comparatively simple approach, in aspect, that no code change is required and it can be implemented directly at database level without disturbing the code.

How to Stream Media from Window PC to Xbox One


XBox One provides features to stream media from your PC and to enjoy the media on common entertainment box i.e. XBox. Below are simple instructions to setup the streaming of media contents from PC to XBox one, along with solution for one commonly faced problem. Hope it will help.

Note: Instructions are given considering window 7 PC.

On XBox One:

  1. Make sure that Xbox One is connected to your home network, either via wired or wireless connection.
  2. Download the ‘Media Player’ app from Xbox Store. This will help you to explore the media on PC remotely. 
  3. Go to into the “Preferences” option through the user settings menu. Make sure that the “PlayTo” streaming option is checked. 




On PC:

  1. Switch over to your PC. 
  2. Go to Start menu and click on “Devices and Printers”.
  3. Use ‘Add a device’ and add Xbox one as one Device. 
  4. Open Window Media Player
  5. Go to ‘Stream’ menu and enable the ‘Allow Remote Control of my Player’ and ‘Automatically Allow Devices to play my Media’ options. 

Leading by Example - An Interesting Approach to Lead

Albert Einstein once said “Setting an example is not the main means of influencing others, it is the only means.”

To compliment this quote, let me tell you a story. Story begins 16 years back when I started working as shop supervisor in a mechanical workshop. Workshop had around 25 skilled technicians. Most of them were in age group of 30s to 50s. It was a challenging job for a 19 year old boy to manage these experienced and skilled workers. Hence I had various wonderful learnings during this job. Following is one of such interesting learning.

Very first day, when I entered in the workshop, I found it very unclean and untidy. There were some obvious reasons for being dirty. Like being a mechanical workshop, there was lot of scrap production daily. Lot of oil was being used for lubrication along with various coolants also. However, still there were good reasons to believe that it can be comparatively cleaner. I felt a need to change or improve. Being very new to the Organization, I decided to wait for sometime. Eventually I started discussing with team members about cleaning the place. One day I got a simply reply from few workers for not cleaning the workshop, that was, "Either we can clean the workshop or can complete our day job. You tell us what we need to do". Sound familiar :). Even today sometimes I heard similar reasoning that, "Either we can improve the code/process, or can make the delivery". I understood the problem and gave it some more time.

One morning, I thought that I should clean my place at least. My place comprised of one table, chair, cupboard, some documents and lot of dirt. I spent 3-4 hours and cleaned it completely. All the workers were looking at me quite surprisingly. At the end of the day, I was happy that I did what I liked and what I should have done. There was no expectation around. Next morning, 2-3 of the workers came to my seat and told me that they also wanted to clean their place. They asked, can I manage the time break for cleaning. I took the responsibility and told that I shall manage it. They called all other workers and discussed with them. That day, all the them worked as a team and cleaned the workshop for 4-5 hours. Results were obviously pleasant. Workshop was completely changed, air was purified. All of the co-workers were appreciating the change themselves. Earlier they had to find a clean place to have their tea, now whole workshop was cleaned enough to sit anywhere.

Vedic Math - Divsion by 9

Let us learn, how to do 'Division' operation using 'Vedic Math'. Conventionally, we do it like following:

Divisor ) Dividend ( Quotient
                ---------
                ---------
             _________
             Remainder

However, in the Vedic process, the format is
Divisor ) Dividend
                --------
           __________________
           Quotient | Remainder

Let us first start with one of the special case of division i.e. Division By 9, a very interesting and simple technique.

When dividing by 9, the remainder is always the digit sum of the original number.

For 2-digit number divided by 9 
To divide ab by 9 : Rewrite ab as a | b . The quotient is a, and the remainder is simply a + b.

    a  |  b
        |  a
    ---------
    a  | a + b

Examples: 

12 divided by 9
Here quotient = 1 and remainder = 1+2 = 3

23 divided by 9
Here quotient = 2 and remainder = 2+3 = 5

70 divided by 9
Here quotient = 7 and remainder = 7+0 = 7

Now, let us discuss the cases when remainder is greater than 9 :-

86 divided by 9
Here quotient = 8 and remainder= 8+6 = 14 ( >9 )
So we add one in the quotient and becomes 7 ; and
remainder becomes 5, after subtracting 9 from 14

New quotient =  7 and New remainder = 5

75 divided by 9
Here quotient = 7 and remainder = 7+5 = 12 ( >9 )
So, New quotient =  8 and New remainder = 3 (12-9=3)

Also, notice here, that the new remainder is just the digit sum of the old remainder.

For 3-digit number divided by 9

      ab  |  c
        a  | a + b
   ---------------
 ab + a | a + b + c

Quotient: ab + a ; Remainder: a + b + c. However, remember that the remainder should be less than 9. And if remainder is greater than 9; we add 1 to quotient and subtract 9 from the remainder.

Vedic Math - Cube Roots of more than 6-Digit Number - Part III

In this article, we are going to learn an interesting Mathematical technique to find, if the given number is a perfect cube or not. It is very important step while computing cube roots. Infact, before applying any method to find the cube root, we have to check whether it is perfect cube or not and then accordingly we choose the technique. For example, following scenario tells us the importance of finding perfect cube step while computing the cube root.

Example : 1728 has cube root 12 since two groups are 1 and 728. From 728, we derive last digit as                 2 from 1 (first group), we derive first digit as 1.
               So, cube root of 1728 is 12.
 But now, if number is 1278, which again has two groups: 1 and 278. It can derive the same last digit as 2 and first digit as 1 , which implies that cube root of 1278 is 12, which is not true because technique stands true for perfect cube root only.

There is a simple technique to check whether the number is perfect cube or not. For this, we add the digits of the number. See the below chart in which we add the digits of cubes from 1 to 10.

Above example shows that sum of digits of a perfect cube is either 1, 8 or 9. However, it is not true that all numbers which sum to 1,8 or 9, will be perfect cube.

For example,
Sum of digits of 1728 and 1278 are same i.e.(1+7+2+8) = (18) = 9 . But 1278 is not a perfect cube.

Hence if sum of digits of a number is not 1,8 or 9, we are very sure that the number is not a perfect cube. However, a number may not be perfect cube root even if sum of digits is 1,8 or 9. To scrutinize that, we need to apply factorisation. If number is small like 1278, factorisation is good method. See below:


For bigger numbers, factorisation could be time consuming technique. Hence, for large numbers, we shall apply general method of finding the cube of root.

Case 2 : Cube root for all the cubes, whether perfect cubes or not.    (Case 1 discussed in last two articles)
From last two articles, we conclude about the sequence of digits (a+b+c)³ as:
(1) The first place by a³
(2) The second place by 3a2b
(3) The third place by 3ab2+3a2c
(4) The fourth place by 6abc+b³
(5) The fifth place by 3ac2+3b2c
(6) The sixth place by 3bc2
(7) The seventh place by c³ ; and so on.

In 'General Technique', we find Dividends(D), Quotients(Q), and Remainders(R). Steps involved as:
(1) First determine D, Q and R
(2) From the second dividend, no deduction is to be made.
(3) From the third, subtract 3ab2
(4) From the fourth, deduct 6 abc+b³
(5) from the fifth, subtract 3ac2+3b2c
(6) from the sixth, deduct 3bc2
(7) from the seventh, subtract c³. ; and so on.

(a) Quotient(Q) is closest minimum exact cube to the first cube i.e. 'F' term used in last two articles.
(b) And, Reminder(R) is the difference between the first group and closest minimum exact cube.
(c) Dividend(D) is found by multiplying the 'Square of Quotient(Q)' by 3 (Q2*3)

Vedic Math - Cube Roots of more than 6-Digit Number - Part II

In last article, we have discussed the method to find cube root of more than 6-digit numbers; especially the odd numbers. Today we shall discuss the procedure for even numbers. In this procedure, only two extra steps are added, one in the beginning and other at the end. Rest all is same.

Procedure: As first added step, we keep on dividing the number by 8 till we get an odd cube. Following it, same method of successive elimination of the digits will apply. At the end, multiply the cube root by 8 to obtain the cube root of the original number.

Example :  2840362499528
First, we continue without using those two additional steps, which will help you to understand the problems arises while dealing with even cubes.

The cube root of the cube 2,840,362,499,528      (say, F + J + H + M + L )
Here,  N=5       (means that cube root will be of 5 digits number)
L=2                  (i.e. 2³=8, matching with the last digit of the last group '528')
and F=1            (i.e, 1³=1, nearest cube of first group '1')

Step1 : L=2 & L³=8. Subtracting this,              
Step2 : 3L2M=12M (substituting L = 2)
            Hence, 12M = Number ending with 2
    Here M is either '1' or '6'      (ambiguous values)
            Lets take 6  (pure gamble)
     
    Now, Deducting 3L2M = 12M = 72  
Step3 : 3LM2+3L2H = 12H + 216 (substituting L = 2, M = 6)
            12H + 216 = Number ending with 8
      12H = Number ending with 2                                    
            Here H is either '1' or '6'                                      
    Lets take 1 (again gamble)

    Now, Subtract 3LM2+3L2H = 12H + 216
                                                         = 228
Step4 : 3L2J+6LMH+M³ = 12J+12+216          
                                             = 12J+228
        12J+228 = Number ending with 6
12J = Number ending with 8
        Here J is either '4' or '9'
        Lets take J = 4  

Since we already know 'F' , so no need to know the expansion of (F+J+H+M+L)³ 
Therefore, cube root is 14162        (F=1, J=4, H=1, M=6, L=2)

Vedic Math - Cube Roots of more than 6-Digit Number - Part I

Back after a long break. In previous article, we learn how to find the cube root of 4 or 5 or 6 digits perfect cubes. Let us continue it further and discuss how to find the cube root of perfect or imperfect cubes.

In this article, we shall learn to find the cube roots for:
1. Cube root of perfect cubes, for any number of digits.
2. Cube root for all the cubes, whether perfect cubes or not.

To summarize what we have learned till now for cube root:

Arrange the given number in three-digit groups, starting from right to left. A single digit, if any left over at the left hand side, is counted as a simple group itself. The number of  digits in the cube root will be the same as the number of digit-groups  in the given  number itself.

  • 169 will count as 1 group 
  • 1 258 will count as 2 groups 
  • 43 781 will count as 2 groups
  • 2 154 890 will count as 3 groups

If the given number has 'n' digits, its cube root will be having n/3 or (n+1)/3 digits. Also remember few other points from previous article:

The Cubes  of the first nine natural numbers    
1³ = 1        2³ = 8        3³ =27        4³ = 64        5³ = 125        6³ = 216        7³ = 343        8³ = 512         9³ = 729        10³ = 1000
From it, we understand that

  • 1,4,5,6,9,0 numbers repeat themselves in the ending of their cubes
  • 2,3,7 and 8 have their complements from 10, at the end of their cube

Let us start with actual technique now. Any number can be written in an algebraic expression. For example, if arithmetical number is 'dcba', it can be written in algebraic form as:
Algebraic Expression is: a + 10b + l00c + 1000d.

Now if we need to find the cube of a number 'cba', algebraically we can expand it like  (a+10b+102c)3. Let us expand it:
(a+10b+102c) 3 =  a3 + 10 (3a2b) + 102 (3ab2+3a2c) + 103 (b3+6abc) + 104 (3ac2+3b2c) + 105 (3bc2) + 106 (c3)

Now removing the powers of ten and putting the result in algebraic form, it tells us the formation of cube as:
(1) The units' place is determined by a³.
(2) The tens' place is contributed by 3 a2b
(3) The hundreds' place is contributed to by 3ab2 + 3a2c
(4) The thousands' place is formed by b³ + 6abc
(5) The ten thousands' place is given by 3ac2 + 3b2c
(6) The hundred thousands' (lakhs') place is constituted of 3bc2 ; and
(7) The millions' place is formed by c³.

The number of zeroes in the various coefficients of the expanded Algebraic Expression are the basis of the formula / analysis.

Case1 :  Cube root of perfect cubes for any number of digits

Suppose we have a cube number n of any number of digits. To find its cube root, find following:
- The number of groups (N) in cube (as we discussed above to make the sets of 3 digits)
- First digit of cube root denoted as 'F' (Nearest cube root of first group from left)
- Last digit of cube root denoted as 'L' (Cube root of last group from left)
- Middle numbers of cube root(i.e. 'M' or 'H' or 'J'....), we shall find using the procedure.

Following are the steps for the procedure:
(i) From the units' place of given number, subtract the L³ (i.e. a³, refer to algebraic expression above); and that eliminates the last digit of the number.
(ii) From the ten's place, we subtract 3L2M (i.e. 3a2b) and thus eliminate the second last digit (penultimate digit).
(iii) From the hundreds' place, we subtract 3LM2 + 3L2F (i.e. 3ab2 + 3a2c) and hence eliminate the pre-penultimate digit.
and so on

The Beautiful Mind

This is an incident from my childhood. In my school days, I used to wake up very early in the morning, early means 4 am. I learnt this habit from my mother and grandmother. At that time, I had a wish to surprise them by waking up earlier than them. However, challenge was, ‘How I can do it without alarm clock’. I kept on thinking this for many days, and this wish grew stronger in my mind. One day, to my surprise, I woke up earlier than them. This happened for next many next days. Eventually I realized that my strong wish actually made my sub-conscious mind to help me in waking me up at a specific time. Sub-conscious mind is capable to understand the time. Then I experimented by setting different times and that worked. I utilized this amazing power of mind many a times during my college days too.

This is the power of our sub-conscious mind. A small 3 pound organ, but having 1 trillion cells, and 100 billion neurons. It has 10 to the power 1 million ways of wiring itself and can store 1000 TB of information. I still have only 1 TB of external disk. Further, it can process 10 to the power 13 operations per second. Amazing speed. Slowest information speed in mind is around 260 mph, faster than most of the fast cars. We have this kind of powerful organ with us, which if utilized properly, can do amazing things. However, power is good as long as it is in control. Uncontrolled mind can also do ‘not so good’ things. How, let us understand with an example of Robot (equivalent to sub-conscious mind).

Suppose we have an intelligent, self-learner robot. It is being programmed to serve us. One of the important task assigned to it is, to defend us from all external threats and also to give us suggestion in case of any problem. It is doing its duty very well by taking a fight with attackers. Intensity of fight is specified by us.

Now visualize a scenario when this Robot become more intelligent with time. One day, it decides to teach the attacker a better lesson. So it ignores our instruction for intensity of fight, and instead of a simple slap, it breaks the hand of attacker. Although it is doing all that for you, but now it is acting in autonomous mode and hence can take decision which can create problem in real life. This is how our mind does many of the times. That’s why many a times, we overreact to situation, which is not good. Robot is good as long as it is in your control and following your instructions.

Spring RoutingDataSource to Work with Multiple DataSources - II

In previous article, we have discussed about using Spring 'AbstractRoutingDataSource' to dynamically routing the database call to desired database (i.e. data source). This is very handy feature and useful in various scenarios. 

One important use case can be in multi-tenant application. In multi-tenant application, one of the database design consideration can be to use separate database for each tenant. It helps to keep the data of each tenant separate, and would be good for performance also. And, good design would require to use the same application layer code to work with all tenant and their specific database. In this scenario, use of 'AbstractRoutingDataSource' can help to achieve these design considerations. Extend the AbstractRoutingDataSource to create a custom 'MultiTenantRoutingDataSource'. Use a thread local application context state holder to maintain the tenant state for current flow. Use this thread local context in extended 'MultiTenantRoutingDataSource' to return the tenant specific data source. Again keep in consideration please, that Routing Data Source will be invoked by TransactionManager before starting the transaction only. Once transaction is started, data source can not be changed. 

Let us see the example of data source definition in Context for dynamically switching between OLTP and Reporting Database: 

<bean id="dataSourceOltp" name="oltpDataSource" class="..PooledDataSource">
<property name="targetDataSource">
<jee:jndi-lookup jndi-name="jdbc/DB" cache="true" />
</property>
</bean>


<bean id="dataSourceReportDb" name="dataSourceReportDb" class="..PooledDataSource">
<property name="targetDataSource">
<jee:jndi-lookup jndi-name="jdbc/ReportDB" cache="true" />
</property>
</bean>

<bean id="customDataSource" class="com.a.b.common.db.DBCustomRoutingDataSource ">
<property name="targetDataSources">
<map key-type="com.a.b.util.DataSourceConstants">
<entry key="OLTP_DB" value-ref="dataSourceOltp"/>
<entry key="REPORT_DB" value-ref="dataSourceReportDb"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSourceOltp"/>
</bean>

<bean id="txAwareCustomDS" name="txAwareCustomDS" class="org.springframework.jdbc.datasource.Transac tionAwareDataSourceProxy">
<property name="targetDataSource" ref="customDataSource">
</property>
</bean>

<bean id="CustomTxManager" class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
<property name="dataSource" ref="txAwareCustomDS" />
</bean>


In above context definition, we may define data sources for multiple tenants in place of OLTP or Reporting Database. To extend the design further for multiple tenants, definition can be made dynamic.

Important Note:

If you are using TransactionAwareDataSourceProxy with DataSourceTransactionManager, it should always be the outer most wrapper in the hierarchy. Keep data sources hierarchy as following:

TransactionAwareDataSourceProxy > Routing Data Source > Lazy Data Source > Pooled Data Source

Reason is, DataSourceTransactionManager will never work with TransactionAwareDataSourceProxy, rather it picks the wrapped data source to work upon. DataSourceTransactionManager has special handling for TransactionAwareDataSourceProxy to work with actual DataSource wrapped in it. So it is important to keep the TransactionAwareDataSourceProxy as outer most layer. Or otherwise, DataSourceTransactionManager will not be able to handle the transactions properly. Read more at following links:

Java Technology Enthusiasts - How to set Transaction Manager Programatically
Spring Forum - Problem in Managing Managing Transaction with AbstractRoutingDataSource

------------------------------------------------------------------------------------------------------------
Hope it will help. Please share comments for addition. You may contribute by:
  • Posting your comments which will add value to the article contents
  • Posting the article link on Social Media using 'Social Media Bookmark' bar
  • Consider joining us at 'Java Technology Enthusiasts' linkedin group to participate in discussions
  • Connecting with 'VedantaTree' on Facebook

New Version 3.1 of ExpressionOasis Released

New version 3.1 of ExpressionOasis has been released.

Release Notes: 

  1. Grammar class is made configurable now. Developers can implement custom Grammar class and can specify this in configuration. ExpressionOasis will pick this at runtime. 
  2. Made grammar.xml path configurable by exporting it to config.xml
  3. Improved code of ExpressionFactory to improve the performance
  4. Defined new way to add function definition by providing new API in Grammar interface 
  5. Exposed Grammar through ExpressionEngine Class, so that user can get the metadata if required and also can add specific metadata like custom function identification etc
  6. Added three new String operation expressions > startsWith, endsWith, contains (Contribution by Girish Kumar) 
  7. Made config.xml path configurable from System Property (Contribution by Girish Kumar)
  8. Improved documentation
Download:
In case of any issue or any new requirement, please log it here. If you are extending the framework to add new features, please consider to contribute these back to the project. It will help everybody whosoever is using the ExpressionOasis.

Follow us: