10 Haziran 2009 Çarşamba

Obtaining Information About The Current User In Spring Security

The details of the principal currently interacting with the application is stored inside SecurityContextHolder. Spring Security uses an Authentication object to represent this information. Use the following code block -from anywhere in your application- to obtain the name and the password of the currently authenticated user:

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
UserDetails principal = (UserDetails) authentication.getPrincipal();
String username = principal.getUsername();
String password = principal.getPassword();


06 Nisan 2009 Pazartesi

Oracle XE'de HTTP Portu Nasıl Değiştirilir?

Oracle XE, standart olarak 8080 no'lu HTTP portu olarak kullanır. Bu portu JBoss ve Tomcat de HTTP portu olarak kullandığı için, Oracle ile beraber bir application server çalıştırmak için, ya application server'in ya da Oracle'in HTTP portunun değiştirilmesi gerekiyor.

Oracle'de bu portu değiştirmek icin dbms_xdb.sethttpport() fonksiyonunu kullanabilirsiniz. Database'e system kullanıcısıyla giriş yaptıktan sonra aşağıdaki şekilde çalıştırabilirsiniz:

EXEC dbms_xdb.sethttpport(8090);

31 Mart 2009 Salı

Intellij Knows How To "Extract Method"

Intellij has a great refactoring feature called "Extract Method". You can convert a code piece into a method using this feature. In this process, Intellij can handle all method parameters and return values. You can use this feature first by selecting the code piece you wish to convert to method, and press CTRL+ALT+M.

I will explain what impressed me most about this feature using following example. In the following code, I am not comfortable with the first try/catch block and I want to hide this dirty code inside a method.
 1     public static void main(String[] args) {
2 Connection conn = null;
3
4
//I am not comfortable with following try/catch block
5 try {
6 Class.forName("com.mysql.jdbc.Driver");
7 } catch (ClassNotFoundException e) {
8 e.printStackTrace();
9 System.out.println("Driver not found");
10 return;
11 }
12
13
try {
14 conn = DriverManager.getConnection(
15 "jdbc:mysql://localhost/petshop", "root", "changeme");
16 Statement statement = conn.createStatement();
17 } catch (SQLException e) {
18 e.printStackTrace();
19 }
20 }
21
To achieve this, position the cursor over 5th line and press CTRL+W. By the way, this is another nice Intellij feature called "Incremental Select" :).



Then, press CTRL+ALT+M. Notice that, the return type of the method is boolean. Click OK and see what we'll get:


Following is the refactored version of our code. Let's see what happened. Intellij put the code inside a method which returns boolean and wrapped the method call inside an if statement. This was the only possible solution which wouldn't violate the flow of program:).

 1     public static void main(String[] args) {
2 Connection conn = null;
3
4
//I am comfortable now
5 if (lookupClass()) return;
6
7
try {
8 conn = DriverManager.getConnection(
9 "jdbc:mysql://localhost/petshop", "root", "changeme");
10 Statement statement = conn.createStatement();
11 } catch (SQLException e) {
12 e.printStackTrace();
13 }
14 }
15
16
private static boolean lookupClass() {
17 try {
18 Class.forName("com.mysql.jdbc.Driver");
19 } catch (ClassNotFoundException e) {
20 e.printStackTrace();
21 System.out.println("Driver not found");
22 return true;
23 }
24 return false;
25 }
26

25 Ocak 2009 Pazar

The Easiest Java Tab Implementation With Close Button

Unfortunately, the current implementation of JTabbedPane class in Swing API does not contain a close button. You have to implement this feature by yourself. Here, I have a simple JTabbedPane implementation with a close button. I used MetalIconFactory.InternalFrameCloseIcon class in the close button. Because this is a private inner class, you need to use MetalIconFactory.getInternalFrameCloseIcon(int iconSize). Check the following screenshots to see how a JTabbedPane and a CloseButtonTabbedPane looks like:

A tabbed pane with default JTabbedPane implementation, shows up as below:



A tabbed pane with CloseButtonTabbedPane implementation, shows up as below:





Click here to download the source code of CloseButtonTabbedPane here.

The advantages of this implementation is:
  • You don't need an image for close button. Instead you use an image provided by Java API.
  • Everything you need is packaged inside just 1 class.

04 Kasım 2008 Salı

How To Disable Corcurrent Logins in Spring-Security

Add the following listener to your web.xml file to keep Spring Security updated about session lifecycle events:

<listener>
<listener-class> org.springframework.security.ui.session.HttpSessionEventPublisher </listener-class>

</listener>



Then add the following line to your application context file:

<http>
...
<concurrent-session-control max-sessions="1" />
</http>



This prevents concurrent logins. A second login will cause the first to be invalidated. To prevent a second login, use following configuration:

<http>
...
<concurrent-session-control max-sessions="1" exception-if-maximum-exceeded="true"/>
</http>



The second login will then be rejected.

11 Ekim 2008 Cumartesi

Create XSL Document Using An Existing XML Document

To create an xml schema document(XSL), you should know XSL specification. But, thank goodness, there is another time saving option. Intellij IDEA does the job for us, without any effort. Intellij IDEA is capable of generating XSL documents from existing XML files. Assuming that we have such an XML document as follows:
<table>
<column>
<name>Name</name>
<type>String</type>
</column>
<column>
<name>Surname</name>
<type>String</type>
</column>
<column>
<name>Birthday</name>
<type>Date</type>
</column>
</table>


Because we don't have an XSD file for our XML file, Intellij complains about this and cannot make syntax-highlighting. Let's resolve this issue. Go to Tools/Generate Xml Schema From Document menu option:



Then, change the filename to "xml-format.xsd" and change the value of "Detect enumerations limit" to any value less than 3. This is because, we have 3 column regions in the XML document and the values inside the column tags are not enumerations. If we don't change this value to something lower than 3, xsd generator will interpret the values inside the column tag as enumerations, and generate the xsd file based on this interpretation. So, change it to 1, for example. Then click OK.



You will get the following XSD document as a result:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="table" type="tableType"/>
<xs:complexType name="columnType">
<xs:sequence>
<xs:element type="xs:string" name="name"/>
<xs:element name="type">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="String"/>
<xs:enumeration value="Date"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="tableType">
<xs:sequence>
<xs:element type="columnType" name="column" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

You can also change the format of the generated XSD document, using "Design Type" area in "Generate Schema From Instance Document" dialog box. There are 3 available choices:



But we are not done yet. We should tell our XML document the location of its schema document(XSD) which we just created before. Assuming that, our XML and XSD files are on the same path, change the first line of the XML as follows:

<table xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="xml-format.xsd">
<column>
<name>Namename>
<type>Stringtype>
column>
<column>
<name>Surnamename>
<type>Stringtype>
column>
<column>
<name>Birthdayname>
<type>Datetype>
column>
table>

Now, our xml editor is able to make syntax-highlighting, using the newly generated XSD file.



And now, Ctrl+Space works also. We may not write erronous XML files any more:).

09 Ekim 2008 Perşembe

NLS_DATE_FORMAT Kullanımı

SQL> select sysdate from dual;

SYSDATE
----------
09/10/2008

SQL> alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss';

Oturum değiştirildi.

SQL> select sysdate from dual;

SYSDATE
-------------------
2008-10-09 17:27:38