TransformerFactory tFactory = TransformerFactory.newInstance();
Source xslSource = new StreamSource(new File(CORE_DIR+"/conf/xslt","media.xsl"));
transformer = tFactory.newTransformer(xslSource);
StringReader sr = new StringReader(result);
StringWriter sw = new StringWriter();
Source xmlInput = new StreamSource();
StreamResult xmlOutput = new StreamResult(sw);
transformer.transform(xmlInput, xmlOutput);
//System.out.println(sr);
System.out.println(sw);
Thursday, April 23, 2009
Wednesday, April 8, 2009
Spring Custom Extensions
- CustomPropertyPlaceHolderConfigurer
@Override
public void setLocations(Resource[] locations) {
Resource[] newLocations =new Resource[locations.length+1];
for(int i=0;i
}
newLocations[locations.length]= new ClassPathResource("env/"+System.getProperty(PROP_MODE)+".properties");
super.setLocations(newLocations);
}
}
- CustomContextLoaderListener
private Log log = LogFactory.getLog(getClass());
@Override
public void contextInitialized(ServletContextEvent event) {
String port=null;
Parameters para = (Parameters)event.getServletContext().getAttribute(PROP_RS_PARAMS);
for(String p:Arrays.asList(para.getPorts())){
if(p.contains("public")){
port=p.substring(0,p.indexOf("/"));
break;
}
}
log.info("Startup Params: \nMODE .... "+para.getMode()+ "\nHOST .... "+para.getHost()+"\nPORT ... "+port);
System.setProperty(PROP_MODE, para.getMode());
System.setProperty(PROP_HOST_IP, para.getHost());
System.setProperty(PROP_HOST_PORT, port);
super.contextInitialized(event);
}
}
Http Client Usage
public String query(String host, int port, String queryString) throws SolrException {
HostConfiguration hostConfig = new HostConfiguration();
hostConfig.setHost(host,port);
client.setHostConfiguration(hostConfig);
GetMethod method = new GetMethod(queryString);
String buffer;
StringBuilder response;
try {
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
log.error("Method failed: " + method.getStatusLine());
throw new HttpException(method.getStatusLine().toString());
}else{
BufferedReader in = new BufferedReader(new InputStreamReader
(method.getResponseBodyAsStream(), method.getResponseCharSet()));
response = new StringBuilder();
while( (buffer=in.readLine()) != null){
response.append(buffer);
}
}
} catch (HttpException e) {
log.error("Fatal protocol violation: ",e);
throw new SolrException(e);
} catch (IOException e) {
log.error("Fatal transport error: ", e);
throw new SolrException(e);
} finally {
method.releaseConnection();
}
return response.toString();
}
HostConfiguration hostConfig = new HostConfiguration();
hostConfig.setHost(host,port);
client.setHostConfiguration(hostConfig);
GetMethod method = new GetMethod(queryString);
String buffer;
StringBuilder response;
try {
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
log.error("Method failed: " + method.getStatusLine());
throw new HttpException(method.getStatusLine().toString());
}else{
BufferedReader in = new BufferedReader(new InputStreamReader
(method.getResponseBodyAsStream(), method.getResponseCharSet()));
response = new StringBuilder();
while( (buffer=in.readLine()) != null){
response.append(buffer);
}
}
} catch (HttpException e) {
log.error("Fatal protocol violation: ",e);
throw new SolrException(e);
} catch (IOException e) {
log.error("Fatal transport error: ", e);
throw new SolrException(e);
} finally {
method.releaseConnection();
}
return response.toString();
}
Is Host Reachable
public static boolean isHostReachable(String ip){
boolean pingable = false;
String[] ips = StringUtils.split(ip,".");
byte[] addr2 = new byte[ips.length];
for(int i=0,j; i j = Integer.parseInt(ips[i]);
addr2[i]=(byte)j;
}
try {
pingable = InetAddress.getByAddress(addr2).isReachable(1500);
} catch (UnknownHostException e) {
pingable = false;
log.error(ip + " Indexer Host not reachabe.");
} catch (IOException e) {
pingable = false;
log.error(ip + " Indexer Host not reachabe.");
}
return pingable;
}
boolean pingable = false;
String[] ips = StringUtils.split(ip,".");
byte[] addr2 = new byte[ips.length];
for(int i=0,j; i
addr2[i]=(byte)j;
}
try {
pingable = InetAddress.getByAddress(addr2).isReachable(1500);
} catch (UnknownHostException e) {
pingable = false;
log.error(ip + " Indexer Host not reachabe.");
} catch (IOException e) {
pingable = false;
log.error(ip + " Indexer Host not reachabe.");
}
return pingable;
}
Get Local Host LAN Address
/**
* This method is intended for Linux Systems, as Linux fails to provide the external IP
* when using InetAddress.getLocalHost(). It instead provides the loopback address.
*
* @return
* @throws UnknownHostException
*/
private static InetAddress getLocalHostLANAddress() throws UnknownHostException {
try {
InetAddress candidateAddress = null;
// Iterate all NICs (network interface cards)...
for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {
NetworkInterface iface = ifaces.nextElement();
// Iterate all IP addresses assigned to each card...
for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
InetAddress inetAddr = inetAddrs.nextElement();
if (!inetAddr.isLoopbackAddress()) {
if (inetAddr.isSiteLocalAddress()) {
// Found non-loopback site-local address. Return it immediately...
return inetAddr;
}
else if (candidateAddress == null) {
// Found non-loopback address, but not necessarily site-local.
// Store it as a candidate to be returned if site-local address is not subsequently found...
candidateAddress = inetAddr;
// Note that we don't repeatedly assign non-loopback non-site-local addresses as candidates,
// only the first. For subsequent iterations, candidate will be non-null.
}
}
}
}
if (candidateAddress != null) {
// We did not find a site-local address, but we found some other non-loopback address.
// Server might have a non-site-local address assigned to its NIC (or it might be running
// IPv6 which deprecates the "site-local" concept).
// Return this non-loopback candidate address...
return candidateAddress;
}
// At this point, we did not find a non-loopback address.
// Fall back to returning whatever InetAddress.getLocalHost() returns...
InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
if (jdkSuppliedAddress == null) {
throw new UnknownHostException("The JDK InetAddress.getLocalHost() method unexpectedly returned null.");
}
return jdkSuppliedAddress;
}
catch (Exception e) {
UnknownHostException unknownHostException = new UnknownHostException("Failed to determine LAN address: " + e);
unknownHostException.initCause(e);
throw unknownHostException;
}
}
* This method is intended for Linux Systems, as Linux fails to provide the external IP
* when using InetAddress.getLocalHost(). It instead provides the loopback address.
*
* @return
* @throws UnknownHostException
*/
private static InetAddress getLocalHostLANAddress() throws UnknownHostException {
try {
InetAddress candidateAddress = null;
// Iterate all NICs (network interface cards)...
for (Enumeration
NetworkInterface iface = ifaces.nextElement();
// Iterate all IP addresses assigned to each card...
for (Enumeration
InetAddress inetAddr = inetAddrs.nextElement();
if (!inetAddr.isLoopbackAddress()) {
if (inetAddr.isSiteLocalAddress()) {
// Found non-loopback site-local address. Return it immediately...
return inetAddr;
}
else if (candidateAddress == null) {
// Found non-loopback address, but not necessarily site-local.
// Store it as a candidate to be returned if site-local address is not subsequently found...
candidateAddress = inetAddr;
// Note that we don't repeatedly assign non-loopback non-site-local addresses as candidates,
// only the first. For subsequent iterations, candidate will be non-null.
}
}
}
}
if (candidateAddress != null) {
// We did not find a site-local address, but we found some other non-loopback address.
// Server might have a non-site-local address assigned to its NIC (or it might be running
// IPv6 which deprecates the "site-local" concept).
// Return this non-loopback candidate address...
return candidateAddress;
}
// At this point, we did not find a non-loopback address.
// Fall back to returning whatever InetAddress.getLocalHost() returns...
InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
if (jdkSuppliedAddress == null) {
throw new UnknownHostException("The JDK InetAddress.getLocalHost() method unexpectedly returned null.");
}
return jdkSuppliedAddress;
}
catch (Exception e) {
UnknownHostException unknownHostException = new UnknownHostException("Failed to determine LAN address: " + e);
unknownHostException.initCause(e);
throw unknownHostException;
}
}
Spring Config
Web.xml
<.context-param.>
<.param-name.>contextConfigLocation<./param-name.>
<.value.>/WEB-INF/spring/applicationContext.xml<./param-value.>
<./context-param.>
<.context-param.>
<.param-name>log4jExposeWebAppRoot<./param-name>
<.param-value>false<./param-value..>
<./context-param.>
<.servlet>
<.servlet-name.>searchApp<./servlet-name.>
<.servlet-class.>com.ea.sw.search.web.servlet.SearchDispatcherServlet<./servlet-class.>
<.servlet-class.>org.springframework.web.servlet.DispatcherServlet <./servlet-class.>
<.init-param.>
<.param-name.>contextConfigLocation<./param-name.>
<.param-value.>/WEB-INF/spring/webContext.xml<./param-value.>
<./init-param.>
<.load-on-startup.>1<./load-on-startup.>
<./servlet.>
WebContext.xml
<.property name="mappings".>
<.props.>
<.prop key="/all">allSearchSequentialController<./prop.>
<./props>
<./property.>
<.property name="alwaysUseFullPath".>
<.value>true<./value.>
<./property.>
<./bean.>
<.constructor-arg.>
<.list.>
<.ref bean="groupSearchHandler"/.>
<.ref bean="mediaSearchHandler"/.>
<.ref bean="personaSearchHandler"/.>
<./list.>
<./constructor-arg.>
<./bean.>
<.constructor-arg.>
<.list.>
<.value.>classpath:default.properties<./value.>
<.value.>classpath:index.properties<./value.>
<./list.>
<./constructor-arg.>
<./bean.>
<.property name="locations" ref="locations"/.>
<./bean.>
<.property name="properties".>
<.bean class="org.springframework.beans.factory.config.PropertiesFactoryBean".>
<.property name="locations" ref="locations"/.>
<./bean.>
<./property.>
<./bean.>
<.constructor-arg.>
<.map.>
<.entry key="m".><.value>MINUTE<./value.><./entry.>
<.entry key="h".><.value>HOUR<./value.><./entry.>
<.entry key="d".><.value>DAY<./value.><./entry.>
<.entry key="y".><.value>YEAR<./value.><./entry.>
<./map.>
<./constructor-arg.>
<./bean.>
<.property name="driverClassName".><.value.>${jdbc.driverClassName}<./value.><./property.>
<.property name="url".><.value.>${jdbc.url}<./value.><./property.>
<.property name="username".><.value.>${jdbc.username}<./value.><./property.>
<.property name="password".><.value.>${jdbc.password}<./value.><./property.>
<./bean.>
<.property name="dataSource" ref="searchDS"/.>
<.property name="fetchSize" value="${jdbc.fetch.size}"/.>
<./bean.>
<.property name="triggers".>
<.list.>
<.ref bean="mediaIndexTrigger" /.>
<./list.>
<./property.>
<./bean.>
<.bean id="mediaIndexTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean".>
<.property name="startDelay" value="${media.index.start}" /.>
<.property name="repeatInterval" value="${media.cron.interval}" /.>
<.property name="jobDetail".>
<.bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean".>
<.property name="targetObject" ref="mediaIndex" /.>
<.property name="targetMethod" value="index" /.>
<.property name="concurrent" value="false" /.>
<./bean.>
<./property.>
<./bean.>
<.bean id="mediaIndex" class="com.ea.sw.search.indexer.impl.DefaultIndexImpl".>
<./bean.>
- Context Param
<.param-name.>contextConfigLocation<./param-name.>
<.value.>/WEB-INF/spring/applicationContext.xml<./param-value.>
<./context-param.>
<.context-param.>
<.param-name>log4jExposeWebAppRoot<./param-name>
<.param-value>false<./param-value..>
<./context-param.>
Dispatcher Servlet
<.servlet-name.>searchApp<./servlet-name.>
<.servlet-class.>com.ea.sw.search.web.servlet.SearchDispatcherServlet<./servlet-class.>
<.param-name.>contextConfigLocation<./param-name.>
<.param-value.>/WEB-INF/spring/webContext.xml<./param-value.>
<./init-param.>
<.load-on-startup.>1<./load-on-startup.>
<./servlet.>
WebContext.xml
- URL Mapping
<.property name="mappings".>
<.props.>
<.prop key="/all">allSearchSequentialController<./prop.>
<./props>
<./property.>
<.property name="alwaysUseFullPath".>
<.value>true<./value.>
<./property.>
<./bean.>
- ArrayList
<.constructor-arg.>
<.list.>
<.ref bean="groupSearchHandler"/.>
<.ref bean="mediaSearchHandler"/.>
<.ref bean="personaSearchHandler"/.>
<./list.>
<./constructor-arg.>
<./bean.>
- ArrayList - Load Properties
<.constructor-arg.>
<.list.>
<.value.>classpath:default.properties<./value.>
<.value.>classpath:index.properties<./value.>
<./list.>
<./constructor-arg.>
<./bean.>
- Property Placeholder Config
<.property name="locations" ref="locations"/.>
<./bean.>
- Application Properties
<.property name="properties".>
<.bean class="org.springframework.beans.factory.config.PropertiesFactoryBean".>
<.property name="locations" ref="locations"/.>
<./bean.>
<./property.>
<./bean.>
- Map
<.constructor-arg.>
<.map.>
<.entry key="m".><.value>MINUTE<./value.><./entry.>
<.entry key="h".><.value>HOUR<./value.><./entry.>
<.entry key="d".><.value>DAY<./value.><./entry.>
<.entry key="y".><.value>YEAR<./value.><./entry.>
<./map.>
<./constructor-arg.>
<./bean.>
- Data Source
<.property name="driverClassName".><.value.>${jdbc.driverClassName}<./value.><./property.>
<.property name="url".><.value.>${jdbc.url}<./value.><./property.>
<.property name="username".><.value.>${jdbc.username}<./value.><./property.>
<.property name="password".><.value.>${jdbc.password}<./value.><./property.>
<./bean.>
- JDBC Templae
<.property name="dataSource" ref="searchDS"/.>
<.property name="fetchSize" value="${jdbc.fetch.size}"/.>
<./bean.>
- Scheduler Configurations
<.property name="triggers".>
<.list.>
<.ref bean="mediaIndexTrigger" /.>
<./list.>
<./property.>
<./bean.>
<.bean id="mediaIndexTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean".>
<.property name="startDelay" value="${media.index.start}" /.>
<.property name="repeatInterval" value="${media.cron.interval}" /.>
<.property name="jobDetail".>
<.bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean".>
<.property name="targetObject" ref="mediaIndex" /.>
<.property name="targetMethod" value="index" /.>
<.property name="concurrent" value="false" /.>
<./bean.>
<./property.>
<./bean.>
<.bean id="mediaIndex" class="com.ea.sw.search.indexer.impl.DefaultIndexImpl".>
<./bean.>
Subscribe to:
Posts (Atom)