Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch07] Implemented OxmSqlService using spring oxm abstraction layer.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed Oct 22, 2017
1 parent f6c2f2a commit 6eaf6db
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
75 changes: 75 additions & 0 deletions src/main/java/ch07/springbook/sql/OxmSqlService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package ch07.springbook.sql;

import java.io.IOException;

import javax.annotation.PostConstruct;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

import ch07.springbook.sql.registry.HashMapSqlRegistry;
import org.springframework.oxm.Unmarshaller;

import ch07.springbook.sql.reader.SqlReader;
import ch07.springbook.sql.reader.jaxb.SqlType;
import ch07.springbook.sql.reader.jaxb.Sqlmap;
import ch07.springbook.sql.registry.SqlRegistry;

public class OxmSqlService implements SqlService {

private final BaseSqlService baseSqlService = new BaseSqlService();
private final OxmSqlReader oxmSqlReader = new OxmSqlReader();

private SqlRegistry sqlRegistry = new HashMapSqlRegistry();

private static class OxmSqlReader implements SqlReader {

private final static String DEFAULT_SQLMAP_FILE = "/sql/sqlmap.xml";

private Unmarshaller unmarshaller;
private String sqlMapFile = DEFAULT_SQLMAP_FILE;

public void setUnmarshaller(Unmarshaller unmarshaller) {
this.unmarshaller = unmarshaller;
}

public void setSqlMapFile(String sqlMapFile) {
this.sqlMapFile = sqlMapFile;
}

@Override
public void read(SqlRegistry sqlRegistry) {
try {
Source source = new StreamSource(getClass().getResourceAsStream(sqlMapFile));
Sqlmap sqlmap = (Sqlmap)unmarshaller.unmarshal(source);

for (SqlType sql : sqlmap.getSql()) {
sqlRegistry.registerSql(sql.getKey(), sql.getValue());
}

} catch (IOException e) {
throw new IllegalArgumentException(sqlMapFile + " not found.", e);
}
}
}

@PostConstruct
public void loadSql() {
baseSqlService.setSqlReader(oxmSqlReader);
baseSqlService.setSqlRegistry(sqlRegistry);

baseSqlService.loadSql();
}

public void setUnmarshaller(Unmarshaller unmarshaller) {
oxmSqlReader.setUnmarshaller(unmarshaller);
}

public void setSqlmapFile(String sqlMapFile) {
oxmSqlReader.setSqlMapFile(sqlMapFile);
}

@Override
public String getSql(String key) throws SqlRetrievalFailureException {
return baseSqlService.getSql(key);
}
}
9 changes: 6 additions & 3 deletions src/main/resources/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@

<bean id="testUserService" class="ch01.springbook.user.UserServiceTest$TestUserService" parent="userService" />

<bean id="sqlService" class="ch07.springbook.sql.DefaultSqlService" >
<!--<property name="sqlReader" ref="sqlReader" />
<property name="sqlRegistry" ref="sqlRegistry" /> -->
<bean id="sqlService" class="ch07.springbook.sql.OxmSqlService" >
<property name="unmarshaller" ref="unmarshaller" />
</bean>

<bean id="unmarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="ch07.springbook.sql.reader.jaxb" />
</bean>

<bean id="sqlReader" class="ch07.springbook.sql.reader.JaxbXmlSqlReader" >
Expand Down

0 comments on commit 6eaf6db

Please sign in to comment.