#include <stdio.h>
#include <iostream>
#include <arpa/inet.h>
#include <netdb.h>
int main() {
char hostName[] = "www.google.com";
struct hostent *host;
if ((host = gethostbyname(hostName)) == NULL) {
fprintf(stderr, "(mini) nslookup failed on '%s'", hostName);
return 1;
}
struct in_addr h_addr;
h_addr.s_addr = *((unsigned long *) host->h_addr_list[0]);
const char *ip = inet_ntoa(h_addr);
std::cout << ip << std::endl;
return 0;
}
Friday, March 25, 2011
Resolve the IP Number from a Host Name
This sample translate the IP number from www.google.com. But you can even use localhost or localmachinename to gets the local or external IP number of the local machine.
Convert Integer to String without itoa
This solution is based from here.
#include <iostream>
#include <sstream>
int main() {
int i = 100;
std::stringstream ss;
ss << i;
fprintf(stderr, ss.str().c_str());
}
GNU/C++ Simple Send Mail
See the Microsoft Visual C++ Simple Send Mail here.
I did the class below for GNU C++ based in these links:
SendMail.h:
Usage:
I did the class below for GNU C++ based in these links:
SendMail.h:
#ifndef _SENDMAILHEADER
#define _SENDMAILHEADER
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string>
#include <string.h>
#define HELO "HELO\r\n"
#define DATA "DATA\r\n"
#define QUIT "QUIT\r\n"
namespace mail {
class SendMail {
private:
int sock;
char buf[BUFSIZ];
void send_socket(char *s)
{
send(sock, s, strlen(s), 0);
//fprintf(stderr, s);
}
void read_socket()
{
int len = recv(sock, buf, BUFSIZ, 0);
//fprintf(stderr, ((std::string)buf).substr(0, len).c_str());
}
public:
char* HostIp;
unsigned short HostPort;
char* To;
char* From;
char* Subject;
char* Message;
SendMail() {
HostPort = 25;
};
~SendMail() { };
void Send() {
struct sockaddr_in hostAddr;
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
fprintf(stderr, "SendMail: socket() failed");
return;
}
hostAddr.sin_family = AF_INET;
hostAddr.sin_addr.s_addr = inet_addr(HostIp);
hostAddr.sin_port = htons(HostPort);
if (connect(sock, (struct sockaddr *) &hostAddr, sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "SendMail: connect() failed");
return;
}
read_socket();
send_socket((char*)HELO);
read_socket();
send_socket((char*)"MAIL FROM: ");
send_socket(From);
send_socket((char*)"\r\n");
read_socket();
send_socket((char*)"VRFY ");
send_socket(From);
send_socket((char*)"\r\n");
read_socket();
send_socket((char*)"RCPT TO: ");
send_socket(To);
send_socket((char*)"\r\n");
read_socket();
send_socket((char*)DATA);
send_socket((char*)"Subject: ");
send_socket(Subject);
send_socket((char*)"\r\n");
read_socket();
send_socket(Message);
send_socket((char*)"\r\n");
send_socket((char*)".\r\n");
read_socket();
send_socket((char*)QUIT);
read_socket();
close(sock);
};
};
}
#endif
Usage:
#include "SendMail.h"
int main(int argc, char** argv) {
mail::SendMail* sendmail = new mail::SendMail();
sendmail->HostIp = (char*)"192.168.1.10";
sendmail->From = (char*)"mail@address.com";
sendmail->To = (char*)"mail@address.com";
sendmail->Subject = (char*)"Test!";
sendmail->Message = (char*)"Content:\nTest 123...";
sendmail->Send();
return 0;
}
VC++ Simple Send Mail
See the GNU/C++ Simple Send Mail here.
I did the class below for Visual C++ based in these links:
SendMail.h:
Usage:
I did the class below for Visual C++ based in these links:
SendMail.h:
#ifndef _SENDMAILHEADER
#define _SENDMAILHEADER
#include "stdafx.h"
#include <stdio.h>
#include <Winsock2.h>
#include <Windows.h>
#include <string>
#pragma comment(lib, "ws2_32.lib")
#define HELO "HELO\r\n"
#define DATA "DATA\r\n"
#define QUIT "QUIT\r\n"
namespace mail {
class SendMail {
private:
int sock;
char buf[BUFSIZ];
void send_socket(char *s)
{
send(sock, s, strlen(s), 0);
//fprintf(stderr, s);
}
void read_socket()
{
int len = recv(sock, buf, BUFSIZ, 0);
//fprintf(stderr, ((std::string)buf).substr(0, len).c_str());
}
public:
char* HostIp;
unsigned short HostPort;
char* To;
char* From;
char* Subject;
char* Message;
SendMail() {
HostPort = 25;
};
~SendMail() { };
void Send() {
struct sockaddr_in hostAddr;
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
fprintf(stderr, "SendMail: WSAStartup() failed");
return;
}
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
fprintf(stderr, "SendMail: socket() failed");
return;
}
memset(&hostAddr, 0, sizeof(hostAddr));
hostAddr.sin_family = AF_INET;
hostAddr.sin_addr.s_addr = inet_addr(HostIp);
hostAddr.sin_port = htons(HostPort);
if (connect(sock, (struct sockaddr *) &hostAddr, sizeof(hostAddr)) < 0) {
fprintf(stderr, "SendMail: connect() failed");
return;
}
read_socket();
send_socket(HELO);
read_socket();
send_socket("MAIL FROM: ");
send_socket(From);
send_socket("\r\n");
read_socket();
send_socket("VRFY ");
send_socket(From);
send_socket("\r\n");
read_socket();
send_socket("RCPT TO: ");
send_socket(To);
send_socket("\r\n");
read_socket();
send_socket(DATA);
send_socket("Subject: ");
send_socket(Subject);
send_socket("\r\n");
read_socket();
send_socket(Message);
send_socket("\r\n");
send_socket(".\r\n");
read_socket();
send_socket(QUIT);
read_socket();
closesocket(sock);
WSACleanup();
};
};
}
#endif
Usage:
#include "stdafx.h"
#include "SendMail.h"
int main(int argc, char* argv[])
{
mail::SendMail* sendmail = new mail::SendMail();
sendmail->HostIp = "192.168.1.10";
sendmail->From = "mail@address.com";
sendmail->To = "mail@address.com";
sendmail->Subject = "Test!";
sendmail->Message = "Content:\nTest 123...";
sendmail->Send();
return 0;
}
Friday, September 17, 2010
Force JPA load Identity ID after Insert
When you have an ID into your Entity Classe from Database and that is Identity, if you do an insert action the ID of this Entity isn't reloaded by default with the ID that was generated by Database, through JPA.
A sample of an ID of Identity kind:
To refresh the ID value with the ID that was generated by the Database on insert action, you need put more this below in your AbstractFacade.Create(EntityClass):
Full sample of the AbstractFacade.java:
If you publish your JPA Create action in a Web Services, you will need return to client side an Entity instance to client know what is the new ID. Cause the Entity instance of the Client side can't be refresh with changes inside Server.
Like:
A sample of an ID of Identity kind:
@Id
@Basic(optional = false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "Id")
private Integer id;
To refresh the ID value with the ID that was generated by the Database on insert action, you need put more this below in your AbstractFacade.Create(EntityClass):
getEntityManager().flush();
getEntityManager().refresh(entity);
- getEntityManager().flush() - Will do the commit to Database execute the insert action.
- getEntityManager().refresh(entity) - Will refresh the entity object with a new ID that was inserted.
Full sample of the AbstractFacade.java:
package org.test.ejb;
import java.util.List;
import javax.persistence.EntityManager;
public abstract class AbstractFacade {
private Class entityClass;
public AbstractFacade(Class entityClass) {
this.entityClass = entityClass;
}
protected abstract EntityManager getEntityManager();
public void create(T entity) {
getEntityManager().persist(entity);
getEntityManager().flush();
getEntityManager().refresh(entity);
}
public void edit(T entity) {
getEntityManager().merge(entity);
}
public void remove(T entity) {
getEntityManager().remove(getEntityManager().merge(entity));
}
public T find(Object id) {
return getEntityManager().find(entityClass, id);
}
public List findAll() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return getEntityManager().createQuery(cq).getResultList();
}
public List findRange(int[] range) {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
javax.persistence.Query q = getEntityManager().createQuery(cq);
q.setMaxResults(range[1] - range[0]);
q.setFirstResult(range[0]);
return q.getResultList();
}
public int count() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
javax.persistence.criteria.Root rt = cq.from(entityClass);
cq.select(getEntityManager().getCriteriaBuilder().count(rt));
javax.persistence.Query q = getEntityManager().createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
}
}
If you publish your JPA Create action in a Web Services, you will need return to client side an Entity instance to client know what is the new ID. Cause the Entity instance of the Client side can't be refresh with changes inside Server.
Like:
@WebMethod(operationName = "create")
public BusinessEntity create(@WebParam(name = "businessEntity")
BusinessEntity businessEntity) {
ejbRef.create(businessEntity);
return businessEntity;
}
Wednesday, September 15, 2010
Web Service - A cycle is detected in the object graph. This will cause infinitely deep XML...
When a Web Service that uses an EJB that exposes an Entity Class and this Entity Class makes relationships with another Entity Class, may be you will receives this error:
Caused by: javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.SAXException2: A cycle is detected in the object graph. This will cause infinitely deep XML: org.test.data.Business[id=1] -> org.test.data.BusinessType[id=1] -> org.test.data.Business[id=1]]
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:269)
at com.sun.xml.bind.v2.runtime.BridgeImpl.marshal(BridgeImpl.java:100)
at com.sun.xml.bind.api.Bridge.marshal(Bridge.java:141)
at com.sun.xml.ws.message.jaxb.JAXBMessage.writePayloadTo(JAXBMessage.java:317)
... 35 more
Caused by: com.sun.istack.SAXException2: A cycle is detected in the object graph. This will cause infinitely deep XML: org.test.data.Business[id=1] -> org.test.data.BusinessType[id=1] -> org.test.data.Business[id=1]
at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:248)
at com.sun.xml.bind.v2.runtime.XMLSerializer.pushObject(XMLSerializer.java:537)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:631)
at com.sun.xml.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(ArrayElementNodeProperty.java:65)
at com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:168)
at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:155)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:340)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696)
at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:152)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:340)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696)
at com.sun.xml.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(ArrayElementNodeProperty.java:65)
at com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:168)
at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:155)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:340)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:264)
... 38 more
To this error above you can found solutions saying to use this interface:
NetBeans recognize this package and not mark like an error. But on compile time this package does not exist to compiler side. Perhaps you will receive this error when compiles:
/home/eduveks/project/wstest/src/org/test/data/Business.java:8: package com.sun.xml.internal.bind does not exist
import com.sun.xml.internal.bind.CycleRecoverable;
On my view this error is a bug of NetBeans that recognizes this package when isn't a valid package to Java compiler. To test this bug you only need put this "import com.sun.xml.internal.bind.CycleRecoverable;" into any Java file and you will see that NetBeans don't says nothing and accept as well, but if you "Clean and Build" the error will show up.
The correct package is (without .internal):
To uses that package path above you need add it:
Project Properties -> Libraries -> "Add Library...", select JAXB 2.2 and "Add Library".
A sample how to implements the com.sun.xml.bind.CycleRecoverable:
> Business.java
> BusinessType.java
You need mark the IDs with @XmlID(only before the get method, before the variable declaration don't works):
And mark all Collection relationships methods with @XmlIDREF(only before the get method, before the variable declaration don't works):
> BusinessType.java
Sample of the IntegerAdapter.java:
Caused by: javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.SAXException2: A cycle is detected in the object graph. This will cause infinitely deep XML: org.test.data.Business[id=1] -> org.test.data.BusinessType[id=1] -> org.test.data.Business[id=1]]
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:269)
at com.sun.xml.bind.v2.runtime.BridgeImpl.marshal(BridgeImpl.java:100)
at com.sun.xml.bind.api.Bridge.marshal(Bridge.java:141)
at com.sun.xml.ws.message.jaxb.JAXBMessage.writePayloadTo(JAXBMessage.java:317)
... 35 more
Caused by: com.sun.istack.SAXException2: A cycle is detected in the object graph. This will cause infinitely deep XML: org.test.data.Business[id=1] -> org.test.data.BusinessType[id=1] -> org.test.data.Business[id=1]
at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:248)
at com.sun.xml.bind.v2.runtime.XMLSerializer.pushObject(XMLSerializer.java:537)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:631)
at com.sun.xml.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(ArrayElementNodeProperty.java:65)
at com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:168)
at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:155)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:340)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696)
at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:152)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:340)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696)
at com.sun.xml.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(ArrayElementNodeProperty.java:65)
at com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:168)
at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:155)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:340)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:264)
... 38 more
To this error above you can found solutions saying to use this interface:
- com.sun.xml.internal.bind.CycleRecoverable
NetBeans recognize this package and not mark like an error. But on compile time this package does not exist to compiler side. Perhaps you will receive this error when compiles:
/home/eduveks/project/wstest/src/org/test/data/Business.java:8: package com.sun.xml.internal.bind does not exist
import com.sun.xml.internal.bind.CycleRecoverable;
On my view this error is a bug of NetBeans that recognizes this package when isn't a valid package to Java compiler. To test this bug you only need put this "import com.sun.xml.internal.bind.CycleRecoverable;" into any Java file and you will see that NetBeans don't says nothing and accept as well, but if you "Clean and Build" the error will show up.
The correct package is (without .internal):
import com.sun.xml.bind.CycleRecoverable;
To uses that package path above you need add it:
Project Properties -> Libraries -> "Add Library...", select JAXB 2.2 and "Add Library".
A sample how to implements the com.sun.xml.bind.CycleRecoverable:
> Business.java
package org.test.data;
import com.sun.xml.bind.CycleRecoverable;
import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlID;
import javax.xml.bind.annotation.XmlIDREF;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.test.data.adapter.IntegerAdapter;
@Entity
@Table(name = "Business")
@NamedQueries({
@NamedQuery(name = "Business.findAll", query = "SELECT b FROM Business b"),
@NamedQuery(name = "Business.findById", query = "SELECT b FROM Business b WHERE b.id = :id"),
@NamedQuery(name = "Business.findByNome", query = "SELECT b FROM Business b WHERE b.name = :name"),
public class Business implements Serializable, CycleRecoverable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "Id")
private Integer id;
@Basic(optional = false)
@Column(name = "Name")
private String name;
@JoinColumn(name = "BusinessTypeId", referencedColumnName = "Id")
@ManyToOne(optional = false)
private BusinessType businessType;
public Business() {
}
public Business(Integer id) {
this.id = id;
}
public Business(Integer id, String name) {
this.id = id;
this.name = name;
}
@XmlID
@XmlJavaTypeAdapter(value = IntegerAdapter.class, type = String.class)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlIDREF
public BusinessType getBusinessType() {
return businessType;
}
public void setBusinessType(BusinessType businessType) {
this.businessType = businessType;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Business)) {
return false;
}
Business other = (Business) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "org.test.data.Business[id=" + id + "]";
}
@Override
public Object onCycleDetected(Context cntxt) {
System.out.println("CycleRecoverable.onCycleDetected # ".concat(this.toString()));
Business n = new Business();
n.setId(this.getId());
return n;
}
}
> BusinessType.java
package org.test.data;
import com.sun.xml.bind.CycleRecoverable;
import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlID;
import javax.xml.bind.annotation.XmlIDREF;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.test.data.adapter.IntegerAdapter;
@Entity
@Table(name = "BusinessType")
@NamedQueries({
@NamedQuery(name = "BusinessType.findAll", query = "SELECT bt FROM BusinessType bt"),
@NamedQuery(name = "BusinessType.findById", query = "SELECT bt FROM BusinessType bt WHERE bt.id = :id"),
@NamedQuery(name = "BusinessType.findByName", query = "SELECT bt FROM BusinessType bt WHERE bt.name = :name")})
public class BusinessType implements Serializable, CycleRecoverable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "Id")
private Integer id;
@Basic(optional = false)
@Column(name = "Name")
private String name;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "BusinessType")
private Collection businessCollection;
public BusinessType() {
}
public BusinessType(Integer id) {
this.id = id;
}
public BusinessType(Integer id, String name) {
this.id = id;
this.name = name;
}
@XmlID
@XmlJavaTypeAdapter(value = IntegerAdapter.class, type = String.class)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlIDREF
public Collection getBusinessCollection() {
return businessCollection;
}
public void setBusinessCollection(Collection businessCollection) {
this.businessCollection = businessCollection;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof BusinessType)) {
return false;
}
BusinessType other = (BusinessType) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "org.test.data.BusinessType[id=" + id + "]";
}
@Override
public Object onCycleDetected(Context cntxt) {
System.out.println("CycleRecoverable.onCycleDetected # ".concat(this.toString()));
BusinessType n = new BusinessType();
n.setId(this.getId());
return n;
}
}
You need mark the IDs with @XmlID(only before the get method, before the variable declaration don't works):
@XmlID
@XmlJavaTypeAdapter(value = IntegerAdapter.class, type = String.class)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
And mark all Collection relationships methods with @XmlIDREF(only before the get method, before the variable declaration don't works):
> BusinessType.java
@XmlIDREF
public Collection getBusinessCollection() {
return businessCollection;
}
public void setBusinessCollection(Collection businessCollection) {
this.businessCollection = businessCollection;
}
Sample of the IntegerAdapter.java:
package org.test.data.adapter;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class IntegerAdapter extends XmlAdapter {
@Override
public Integer unmarshal(String v) {
return Integer.parseInt(v);
}
@Override
public String marshal(Integer v) {
return v.toString();
}
}
Thursday, July 29, 2010
Automate Command-line Programs
I had to automate a command-line program. Then I did this script like a sample that do auto control to vi.
This way is possible control any kind of command-line program.
If is necessary read some content from program output, you can put the output in a temp file, in this case goes to /tmp/viOutput. See with attention the line of the subprocess.Popen. Another way is use the pr.process.stdout but it dont work fine for me.
The command pr.process.stdin.write(chr(27)) write the "character" ESC, like the keyboard key.
This way is possible control any kind of command-line program.
#!/bin/python
import subprocess
import time
import threading
class ProcessRunner(threading.Thread):
def run(self):
self.process = subprocess.Popen(["vi /tmp/xpto > /tmp/viOutput"], shell = True, bufsize = 1000, stdout = subprocess.PIPE, stdin = subprocess.PIPE, stderr = subprocess.STDOUT)
pr = ProcessRunner()
pr.start()
time.sleep(1)
pr.process.stdin.write("i\n")
pr.process.stdin.flush()
time.sleep(1)
pr.process.stdin.write("Content xpto... :P\n")
pr.process.stdin.flush()
time.sleep(1)
pr.process.stdin.write(chr(27))
pr.process.stdin.flush()
time.sleep(1)
pr.process.stdin.write(":wq\n")
pr.process.stdin.flush()
If is necessary read some content from program output, you can put the output in a temp file, in this case goes to /tmp/viOutput. See with attention the line of the subprocess.Popen. Another way is use the pr.process.stdout but it dont work fine for me.
The command pr.process.stdin.write(chr(27)) write the "character" ESC, like the keyboard key.
Subscribe to:
Posts (Atom)