Fix awsapi issues in Spring wiring

This commit is contained in:
Kelven Yang 2013-01-30 16:43:40 -08:00
parent 4dc63f50a7
commit 526bb01b04
6 changed files with 71 additions and 49 deletions

View File

@ -35,6 +35,8 @@ import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import com.cloud.bridge.persist.dao.CloudStackConfigurationDao;
import com.cloud.bridge.util.ConfigurationHelper;
import com.cloud.utils.LogUtils;
import com.cloud.utils.component.ComponentContext;
import com.cloud.utils.db.DB;
@Component("EC2MainServlet")
@ -61,8 +63,11 @@ public class EC2MainServlet extends HttpServlet{
@DB
public void init( ServletConfig config ) throws ServletException {
try{
LogUtils.initLog4j("log4j-cloud.xml");
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
ConfigurationHelper.preConfigureConfigPathFromServletContext(config.getServletContext());
ConfigurationHelper.preConfigureConfigPathFromServletContext(config.getServletContext());
ComponentContext.initComponentsLifeCycle();
// check if API is enabled
String value = csDao.getConfigValue(ENABLE_EC2_API);
if(value != null){

View File

@ -59,6 +59,7 @@ import org.apache.axis2.databinding.utils.writer.MTOMAwareXMLSerializer;
import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import com.amazon.ec2.AllocateAddressResponse;
import com.amazon.ec2.AssociateAddressResponse;
@ -152,9 +153,6 @@ public class EC2RestServlet extends HttpServlet {
@Inject UserCredentialsDaoImpl ucDao;
@Inject OfferingDaoImpl ofDao;
static UserCredentialsDaoImpl s_ucDao;
static OfferingDaoImpl s_ofDao;
public static final Logger logger = Logger.getLogger(EC2RestServlet.class);
private final OMFactory factory = OMAbstractFactory.getOMFactory();
@ -170,33 +168,15 @@ public class EC2RestServlet extends HttpServlet {
public EC2RestServlet() {
}
@PostConstruct
void initComponent() {
// Servlet injection does not always work for servlet container
// We use a hacking here to initialize static variables at Spring wiring time
if(ucDao != null) {
s_ucDao = ucDao;
} else {
ucDao = s_ucDao;
}
if(ofDao != null) {
s_ofDao = ofDao;
} else {
ofDao = s_ofDao;
}
}
/**
* We build the path to where the keystore holding the WS-Security X509 certificates
* are stored.
*/
@Override
public void init( ServletConfig config ) throws ServletException {
initComponent();
File propertiesFile = ConfigurationHelper.findConfigurationFile("ec2-service.properties");
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
File propertiesFile = ConfigurationHelper.findConfigurationFile("ec2-service.properties");
Properties EC2Prop = null;
if (null != propertiesFile) {

View File

@ -37,6 +37,7 @@ import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.axis2.AxisFault;
import org.apache.log4j.Logger;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
@ -113,6 +114,8 @@ public class S3RestServlet extends HttpServlet {
@Override
public void init( ServletConfig config ) throws ServletException {
try{
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
ConfigurationHelper.preConfigureConfigPathFromServletContext(config.getServletContext());
// check if API is enabled
String value = csDao.getConfigValue(ENABLE_S3_API);

View File

@ -31,11 +31,13 @@ import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import org.apache.axis2.AxisFault;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.springframework.stereotype.Component;
import com.amazon.ec2.AmazonEC2SkeletonInterface;
import com.amazon.s3.AmazonS3SkeletonInterface;
@ -56,10 +58,12 @@ import com.cloud.bridge.util.ConfigurationHelper;
import com.cloud.bridge.util.DateHelper;
import com.cloud.bridge.util.NetHelper;
import com.cloud.bridge.util.OrderedPair;
import com.cloud.utils.component.ManagerBase;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.Transaction;
public class ServiceProvider {
@Component
public class ServiceProvider extends ManagerBase {
protected final static Logger logger = Logger.getLogger(ServiceProvider.class);
@Inject MHostDao mhostDao;
@Inject SHostDao shostDao;
@ -77,8 +81,8 @@ public class ServiceProvider {
private String serviceEndpoint = null;
private String multipartDir = null; // illegal bucket name used as a folder for storing multiparts
private String masterDomain = ".s3.amazonaws.com";
private final S3Engine engine;
private EC2Engine EC2_engine = null;
@Inject private S3Engine engine;
@Inject private EC2Engine EC2_engine;
// -> cache Bucket Policies here so we don't have to load from db on every access
private final Map<String,S3BucketPolicy> policyMap = new HashMap<String,S3BucketPolicy>();
@ -87,26 +91,26 @@ public class ServiceProvider {
// register service implementation object
Transaction txn = Transaction.open(Transaction.AWSAPI_DB);
txn.close();
engine = new S3Engine();
EC2_engine = new EC2Engine();
serviceMap.put(AmazonS3SkeletonInterface.class, new S3SerializableServiceImplementation(engine));
serviceMap.put(AmazonEC2SkeletonInterface.class, new EC2SoapServiceImpl(EC2_engine));
}
public synchronized static ServiceProvider getInstance() {
if(instance == null)
{
try {
instance = new ServiceProvider();
instance.initialize();
} catch(Throwable e) {
logger.error("Unexpected exception " + e.getMessage(), e);
} finally {
}
}
return instance;
}
@PostConstruct
void initComponent() {
instance = this;
}
public boolean configure(String name, Map<String, Object> params)
throws ConfigurationException {
initialize();
return true;
}
public long getManagementHostId() {
// we want to limit mhost within its own session, id of the value will be returned
long mhostId = 0;

View File

@ -26,13 +26,16 @@ import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import javax.inject.Inject;
import javax.naming.ConfigurationException;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.xml.sax.SAXException;
import com.cloud.bridge.model.CloudStackServiceOfferingVO;
@ -69,12 +72,14 @@ import com.cloud.stack.models.CloudStackUser;
import com.cloud.stack.models.CloudStackUserVm;
import com.cloud.stack.models.CloudStackVolume;
import com.cloud.stack.models.CloudStackZone;
import com.cloud.utils.component.ManagerBase;
/**
* EC2Engine processes the ec2 commands and calls their cloudstack analogs
*
*/
public class EC2Engine {
@Component
public class EC2Engine extends ManagerBase {
protected final static Logger logger = Logger.getLogger(EC2Engine.class);
String managementServer = null;
String cloudAPIPort = null;
@ -82,13 +87,27 @@ public class EC2Engine {
@Inject CloudStackSvcOfferingDao scvoDao;
@Inject OfferingDao ofDao;
@Inject CloudStackAccountDao accDao;
private CloudStackApi _eng = null;
private CloudStackAccount currentAccount = null;
public EC2Engine() throws IOException {
loadConfigValues();
}
@Override
public boolean configure(String name, Map<String, Object> params)
throws ConfigurationException {
try {
loadConfigValues();
} catch(IOException e) {
logger.error("EC2Engine Configuration failure", e);
throw new ConfigurationException("EC2Engine configuration failure");
}
return true;
}
/**
* Which management server to we talk to?
@ -2457,5 +2476,4 @@ public class EC2Engine {
}
return resourceTags;
}
}

View File

@ -33,11 +33,13 @@ import java.util.Set;
import java.util.TimeZone;
import java.util.UUID;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.json.simple.parser.ParseException;
import org.springframework.stereotype.Component;
import com.cloud.bridge.io.S3CAStorBucketAdapter;
import com.cloud.bridge.io.S3FileSystemBucketAdapter;
@ -88,19 +90,24 @@ import com.cloud.utils.db.Transaction;
/**
* The CRUD control actions to be invoked from S3BucketAction or S3ObjectAction.
*/
@Component
public class S3Engine {
protected final static Logger logger = Logger.getLogger(S3Engine.class);
@Inject SHostDao shostDao;
@Inject MHostDao mhostDao;
@Inject static BucketPolicyDao bPolicy;
@Inject BucketPolicyDao bPolicy;
@Inject BucketPolicyDao bPolicyDao;
@Inject SBucketDao bucketDao;
@Inject SAclDao aclDao;
@Inject static SAclDao saclDao;
@Inject SAclDao saclDao;
@Inject SObjectDao objectDao;
@Inject SObjectItemDao itemDao;
@Inject SMetaDao metaDao;
@Inject MHostMountDao mountDao;
static SAclDao s_saclDao;
static BucketPolicyDao s_bPolicy;
private final int LOCK_ACQUIRING_TIMEOUT_SECONDS = 10; // ten seconds
private final Map<Integer, S3BucketAdapter> bucketAdapters = new HashMap<Integer, S3BucketAdapter>();
@ -110,6 +117,11 @@ public class S3Engine {
bucketAdapters.put(SHost.STORAGE_HOST_TYPE_CASTOR, new S3CAStorBucketAdapter());
}
@PostConstruct
void init() {
s_saclDao = saclDao;
s_bPolicy = bPolicy;
}
/**
* Return a S3CopyObjectResponse which represents an object being copied from source
@ -1741,13 +1753,13 @@ public class S3Engine {
if ( 0 == userId.length())
{
// Is an anonymous principal ACL set for this <target, targetId>?
if (hasPermission( saclDao.listGrants( target, targetId, "A" ), requestedPermission )) return;
if (hasPermission( s_saclDao.listGrants( target, targetId, "A" ), requestedPermission )) return;
}
else
{
if (hasPermission( saclDao.listGrants( target, targetId, userId ), requestedPermission )) return;
if (hasPermission( s_saclDao.listGrants( target, targetId, userId ), requestedPermission )) return;
// Or alternatively is there is any principal authenticated ACL set for this <target, targetId>?
if (hasPermission( saclDao.listGrants( target, targetId, "*" ), requestedPermission )) return;
if (hasPermission( s_saclDao.listGrants( target, targetId, "*" ), requestedPermission )) return;
}
// No privileges implies that no access is allowed in the case of an anonymous user
throw new PermissionDeniedException( "Access Denied - ACLs do not give user the required permission" );
@ -1771,7 +1783,7 @@ public class S3Engine {
// -> do we have to load it from the database (any other value means there is no policy)?
if (-1 == result.getSecond().intValue())
{
BucketPolicyVO policyvo = bPolicy.getByName(context.getBucketName());
BucketPolicyVO policyvo = s_bPolicy.getByName(context.getBucketName());
String policyInJson = null;
if (null != policyvo)
policyInJson = policyvo.getPolicy();