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

Commit

Permalink
[ch07] use self-reference to separate responsibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed Oct 22, 2017
1 parent ab74a22 commit 523a7ed
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ public SqlRetrievalFailureException(String message) {
public SqlRetrievalFailureException(String message, Throwable cause) {
super(message, cause);
}

public SqlRetrievalFailureException(Throwable cause) {
super(cause);
}
}
60 changes: 47 additions & 13 deletions src/main/java/ch07/springbook/sql/XmlSqlService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,62 @@
import javax.xml.bind.Unmarshaller;

import ch01.springbook.user.dao.UserDao;
import ch07.springbook.sql.reader.SqlReader;
import ch07.springbook.sql.registry.SqlNotFoundException;
import ch07.springbook.sql.registry.SqlRegistry;

public class XmlSqlService implements SqlService, SqlRegistry, SqlReader {

private SqlReader sqlReader;
private SqlRegistry sqlRegistry;

public class XmlSqlService implements SqlService {
private Map<String, String> sqlMap = new HashMap<>();
private String sqlMapFile;

public void setSqlReader(SqlReader sqlReader) {
this.sqlReader = sqlReader;
}

public void setSqlRegistry(SqlRegistry sqlRegistry) {
this.sqlRegistry = sqlRegistry;
}

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

@PostConstruct
public void loadSql() {
sqlReader.read(sqlRegistry);
}

@Override
public String getSql(String key) throws SqlRetrievalFailureException {
try {
return sqlRegistry.findSql(key);
} catch (SqlNotFoundException e) {
throw new SqlRetrievalFailureException(e);
}
}

@Override
public void registerSql(String key, String sql) {
sqlMap.put(key, sql);
}

@Override
public String findSql(String key) throws SqlNotFoundException {
String sql = sqlMap.get(key);

if (sql == null) {
throw new SqlRetrievalFailureException("Can not find appropriate sql statement, key: " + key);
}

return sql;
}

@Override
public void read(SqlRegistry sqlRegistry) {
String contextPath = Sqlmap.class.getPackage().getName();

try {
Expand All @@ -30,22 +75,11 @@ public void loadSql() {
Sqlmap sqlmap = (Sqlmap)unmarshaller.unmarshal(inputStream);

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

} catch (JAXBException e) {
throw new RuntimeException(e);
}
}

@Override
public String getSql(String key) throws SqlRetrievalFailureException {
String sql = sqlMap.get(key);

if (sql == null) {
throw new SqlRetrievalFailureException("Can not find appropriate sql statement, key: " + key);
}

return sql;
}
}
7 changes: 7 additions & 0 deletions src/main/java/ch07/springbook/sql/reader/SqlReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ch07.springbook.sql.reader;

import ch07.springbook.sql.registry.SqlRegistry;

public interface SqlReader {
void read(SqlRegistry sqlRegistry);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ch07.springbook.sql.registry;

public class SqlNotFoundException extends RuntimeException {

public SqlNotFoundException(String message) {
super(message);
}

public SqlNotFoundException(String message, Throwable cause) {
super(message, cause);
}
}
6 changes: 6 additions & 0 deletions src/main/java/ch07/springbook/sql/registry/SqlRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ch07.springbook.sql.registry;

public interface SqlRegistry {
void registerSql(String key, String sql);
String findSql(String key) throws SqlNotFoundException;
}
2 changes: 2 additions & 0 deletions src/main/resources/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
<bean id="testUserService" class="ch01.springbook.user.UserServiceTest$TestUserService" parent="userService" />

<bean id="sqlService" class="ch07.springbook.sql.XmlSqlService" >
<property name="sqlReader" ref="sqlService" />
<property name="sqlRegistry" ref="sqlService" />
<property name="sqlMapFile" value="/sql/sqlmap.xml" />
</bean>
<!--
Expand Down

0 comments on commit 523a7ed

Please sign in to comment.