| 1 |
using System.IO; |
|---|
| 2 |
using System.Collections; |
|---|
| 3 |
using Nuxleus.Extension.Aws; |
|---|
| 4 |
using Nuxleus.Extension.Aws.Sdb; |
|---|
| 5 |
|
|---|
| 6 |
namespace Xameleon.Function |
|---|
| 7 |
{ |
|---|
| 8 |
public class AWSSimpleDB |
|---|
| 9 |
{ |
|---|
| 10 |
|
|---|
| 11 |
Sdb _sdb; |
|---|
| 12 |
|
|---|
| 13 |
public AWSSimpleDB () |
|---|
| 14 |
{ |
|---|
| 15 |
createSdb(System.Environment.GetEnvironmentVariable("SDB_ACCESS_KEY"), System.Environment.GetEnvironmentVariable("SDB_SECRET_KEY")); |
|---|
| 16 |
} |
|---|
| 17 |
|
|---|
| 18 |
public AWSSimpleDB (string awsAccessKey, string awsSecretKey) |
|---|
| 19 |
{ |
|---|
| 20 |
createSdb(awsAccessKey, awsSecretKey); |
|---|
| 21 |
} |
|---|
| 22 |
|
|---|
| 23 |
private void createSdb (string awsAccessKey, string awsSecretKey) |
|---|
| 24 |
{ |
|---|
| 25 |
HttpQueryConnection sdbConnection = new HttpQueryConnection(awsAccessKey, awsSecretKey, "http://sdb.amazonaws.com/"); |
|---|
| 26 |
_sdb = new Sdb(sdbConnection); |
|---|
| 27 |
} |
|---|
| 28 |
|
|---|
| 29 |
public void CreateDomain (string domainName) |
|---|
| 30 |
{ |
|---|
| 31 |
//System.Console.WriteLine(domainName); |
|---|
| 32 |
try |
|---|
| 33 |
{ |
|---|
| 34 |
_sdb.CreateDomain(domainName); |
|---|
| 35 |
} |
|---|
| 36 |
catch (SdbException ex) |
|---|
| 37 |
{ |
|---|
| 38 |
HandleException(ex); |
|---|
| 39 |
} |
|---|
| 40 |
} |
|---|
| 41 |
public void PutAttributes (string domain, string item, ArrayList attributes) |
|---|
| 42 |
{ |
|---|
| 43 |
Item myItem = GetItem(GetDomain(domain), item); |
|---|
| 44 |
|
|---|
| 45 |
try |
|---|
| 46 |
{ |
|---|
| 47 |
myItem.PutAttributes(attributes); |
|---|
| 48 |
} |
|---|
| 49 |
catch (SdbException ex) |
|---|
| 50 |
{ |
|---|
| 51 |
HandleException(ex); |
|---|
| 52 |
} |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
public void DeleteAttribute (string domain, string item, ArrayList attributes) |
|---|
| 56 |
{ |
|---|
| 57 |
Item myItem = GetItem(GetDomain(domain), item); |
|---|
| 58 |
try |
|---|
| 59 |
{ |
|---|
| 60 |
myItem.DeleteAttributes(attributes); |
|---|
| 61 |
} |
|---|
| 62 |
catch (SdbException ex) |
|---|
| 63 |
{ |
|---|
| 64 |
HandleException(ex); |
|---|
| 65 |
} |
|---|
| 66 |
} |
|---|
| 67 |
public void DeleteAttribute (string domain, string item, string attName) |
|---|
| 68 |
{ |
|---|
| 69 |
Item myItem = GetItem(GetDomain(domain), item); |
|---|
| 70 |
ArrayList attribute = new ArrayList(); |
|---|
| 71 |
attribute.Add(new Attribute(attName)); |
|---|
| 72 |
try |
|---|
| 73 |
{ |
|---|
| 74 |
myItem.DeleteAttributes(attribute); |
|---|
| 75 |
} |
|---|
| 76 |
catch (SdbException ex) |
|---|
| 77 |
{ |
|---|
| 78 |
HandleException(ex); |
|---|
| 79 |
} |
|---|
| 80 |
} |
|---|
| 81 |
public void DeleteAttribute (string domain, string item, string attName, string attValue) |
|---|
| 82 |
{ |
|---|
| 83 |
Item myItem = GetItem(GetDomain(domain), item); |
|---|
| 84 |
ArrayList attribute = new ArrayList(); |
|---|
| 85 |
attribute.Add(new Attribute(attName, attValue)); |
|---|
| 86 |
try |
|---|
| 87 |
{ |
|---|
| 88 |
myItem.DeleteAttributes(attribute); |
|---|
| 89 |
} |
|---|
| 90 |
catch (SdbException ex) |
|---|
| 91 |
{ |
|---|
| 92 |
HandleException(ex); |
|---|
| 93 |
} |
|---|
| 94 |
} |
|---|
| 95 |
public void DeleteItem (string domain, string item) |
|---|
| 96 |
{ |
|---|
| 97 |
Item myItem = GetItem(GetDomain(domain), item); |
|---|
| 98 |
try |
|---|
| 99 |
{ |
|---|
| 100 |
DeleteItem(myItem); |
|---|
| 101 |
} |
|---|
| 102 |
catch (SdbException ex) |
|---|
| 103 |
{ |
|---|
| 104 |
HandleException(ex); |
|---|
| 105 |
} |
|---|
| 106 |
} |
|---|
| 107 |
public void DeleteItem (Item item) |
|---|
| 108 |
{ |
|---|
| 109 |
try |
|---|
| 110 |
{ |
|---|
| 111 |
item.DeleteAttributes(); |
|---|
| 112 |
} |
|---|
| 113 |
catch (SdbException ex) |
|---|
| 114 |
{ |
|---|
| 115 |
HandleException(ex); |
|---|
| 116 |
} |
|---|
| 117 |
} |
|---|
| 118 |
public Domain GetDomain (string domainName) |
|---|
| 119 |
{ |
|---|
| 120 |
try |
|---|
| 121 |
{ |
|---|
| 122 |
return _sdb.GetDomain(domainName); |
|---|
| 123 |
} |
|---|
| 124 |
catch (SdbException ex) |
|---|
| 125 |
{ |
|---|
| 126 |
HandleException(ex); |
|---|
| 127 |
throw; |
|---|
| 128 |
} |
|---|
| 129 |
} |
|---|
| 130 |
public GetAttributesResponse GetAttribute (string domain, string item) |
|---|
| 131 |
{ |
|---|
| 132 |
Item myItem = GetItem(GetDomain(domain), item); |
|---|
| 133 |
try |
|---|
| 134 |
{ |
|---|
| 135 |
return GetAttribute(myItem); |
|---|
| 136 |
} |
|---|
| 137 |
catch (SdbException ex) |
|---|
| 138 |
{ |
|---|
| 139 |
HandleException(ex); |
|---|
| 140 |
return null; |
|---|
| 141 |
} |
|---|
| 142 |
} |
|---|
| 143 |
public GetAttributesResponse GetAttribute (Item item) |
|---|
| 144 |
{ |
|---|
| 145 |
try |
|---|
| 146 |
{ |
|---|
| 147 |
return item.GetAttributes(); |
|---|
| 148 |
} |
|---|
| 149 |
catch (SdbException ex) |
|---|
| 150 |
{ |
|---|
| 151 |
HandleException(ex); |
|---|
| 152 |
return null; |
|---|
| 153 |
} |
|---|
| 154 |
} |
|---|
| 155 |
public Item GetItem (string domain, string item) |
|---|
| 156 |
{ |
|---|
| 157 |
Domain myDomain = GetDomain(domain); |
|---|
| 158 |
try |
|---|
| 159 |
{ |
|---|
| 160 |
return GetItem(myDomain, item); |
|---|
| 161 |
} |
|---|
| 162 |
catch (SdbException ex) |
|---|
| 163 |
{ |
|---|
| 164 |
HandleException(ex); |
|---|
| 165 |
throw; |
|---|
| 166 |
} |
|---|
| 167 |
} |
|---|
| 168 |
public Item GetItem (Domain domain, string item) |
|---|
| 169 |
{ |
|---|
| 170 |
try |
|---|
| 171 |
{ |
|---|
| 172 |
return domain.GetItem(item); |
|---|
| 173 |
} |
|---|
| 174 |
catch (SdbException ex) |
|---|
| 175 |
{ |
|---|
| 176 |
HandleException(ex); |
|---|
| 177 |
throw; |
|---|
| 178 |
} |
|---|
| 179 |
} |
|---|
| 180 |
public ArrayList GetArrayList () |
|---|
| 181 |
{ |
|---|
| 182 |
return new ArrayList(); |
|---|
| 183 |
} |
|---|
| 184 |
public void AddAttribute (ArrayList arrayList, string attName, string attValue) |
|---|
| 185 |
{ |
|---|
| 186 |
arrayList.Add(new Attribute(attName, attValue)); |
|---|
| 187 |
} |
|---|
| 188 |
public QueryResponse QueryDomain (string domain, string attName, string attValue) |
|---|
| 189 |
{ |
|---|
| 190 |
Domain myDomain = GetDomain(domain); |
|---|
| 191 |
return myDomain.Query("[\"attName\" = \"attValue\"]"); |
|---|
| 192 |
} |
|---|
| 193 |
public static void HandleException (SdbException ex) |
|---|
| 194 |
{ |
|---|
| 195 |
System.Console.WriteLine("Failure: {0}: {1} ({2})", ex.ErrorCode, ex.Message, ex.RequestId); |
|---|
| 196 |
} |
|---|
| 197 |
|
|---|
| 198 |
} |
|---|
| 199 |
} |
|---|